猫鼬:findByIdAndRemove和forEach在嵌套数组对象中不起作用

时间:2019-09-23 11:08:44

标签: express mongoose foreach mongoose-schema

问题1 :我有一个带有对象数组的Model,一个对象包含多个对象,我想使用findByIdAndRemove删除主对象,但关联的对象没有删除。有人可以给我任何提示以便我解决吗?
blogSchema

var blogSchema=new mongoose.Schema({
    title:String,
    image:String,
    body:{type:String, default:""},
    created:{ type: Date },
  comments:[{
    type:mongoose.Schema.Types.ObjectId,
    ref:'Comment'
  }]
});

评论模式

var commentSchema=mongoose.Schema({
    text:String,
    author:String
})

这是我尝试过的:

app.delete('/blogs/:id',function(req,res,next){
    //destroy blog
    Blog.findByIdAndRemove(req.params.id,function(err){
        if(err){
            res.redirect('/blogs')
        }else{
            res.redirect('/blogs')
        }
    })
})

但是它只会删除与博客相关联的我的博客不注释
问题2 :在渲染show.ejs时,/blogs/:id中的一个小问题。 /blogs/:id GET仅获取一个带有相关注释的博客。如果我在show.ejs中编码了<%=blog.comments%>,那么它将返回

 { author: 'emon ', text: 'comment 1', _id: 5d87027848e59703a4d4a935, __v: 0 },
{ author: 'emon ', text: 'comment 2\r\n', _id: 5d87028848e59703a4d4a936, __v: 0 } 

这意味着它可以正常工作,但是如果我编码的话:

<%blog.comments.forEach(function(comment)%>
<p><strong><%=comment.author%></strong>-<%=comment.text%></p>
)%>

它给了我 SyntaxError
完全错误

    SyntaxError: Unexpected token ; in C:\Users\websi\OneDrive\Desktop\Project\RESTfulBlogApp\v3\views\blogs\show.ejs while compiling ejs

If the above error is not helpful, you may want to try EJS-Lint:
https://github.com/RyanZim/EJS-Lint
Or, if you meant to create an async function, pass async: true as an option.
    at new Function (<anonymous>)
    at Template.compile (C:\Users\websi\OneDrive\Desktop\Project\RESTfulBlogApp\v3\node_modules\ejs\lib\ejs.js:633:12)
    at Object.compile (C:\Users\websi\OneDrive\Desktop\Project\RESTfulBlogApp\v3\node_modules\ejs\lib\ejs.js:392:16)
    at handleCache (C:\Users\websi\OneDrive\Desktop\Project\RESTfulBlogApp\v3\node_modules\ejs\lib\ejs.js:215:18)
    at tryHandleCache (C:\Users\websi\OneDrive\Desktop\Project\RESTfulBlogApp\v3\node_modules\ejs\lib\ejs.js:254:16)
    at View.exports.renderFile [as engine] (C:\Users\websi\OneDrive\Desktop\Project\RESTfulBlogApp\v3\node_modules\ejs\lib\ejs.js:485:10)
    at View.render (C:\Users\websi\OneDrive\Desktop\Project\RESTfulBlogApp\v3\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\websi\OneDrive\Desktop\Project\RESTfulBlogApp\v3\node_modules\express\lib\application.js:640:10)
    at Function.render (C:\Users\websi\OneDrive\Desktop\Project\RESTfulBlogApp\v3\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (C:\Users\websi\OneDrive\Desktop\Project\RESTfulBlogApp\v3\node_modules\express\lib\response.js:1012:7)
    at C:\Users\websi\OneDrive\Desktop\Project\RESTfulBlogApp\v3\app.js:77:8
    at C:\Users\websi\OneDrive\Desktop\Project\RESTfulBlogApp\v3\node_modules\mongoose\lib\model.js:4616:16
    at C:\Users\websi\OneDrive\Desktop\Project\RESTfulBlogApp\v3\node_modules\mongoose\lib\utils.js:264:16
    at C:\Users\websi\OneDrive\Desktop\Project\RESTfulBlogApp\v3\node_modules\mongoose\lib\query.js:4320:11
    at C:\Users\websi\OneDrive\Desktop\Project\RESTfulBlogApp\v3\node_modules\kareem\index.js:135:16
    at processTicksAndRejections (internal/process/task_queues.js:75:11)

任何帮助将不胜感激。很抱歉一次提出太多问题。谢谢你的时间。
谢谢。

2 个答案:

答案 0 :(得分:2)

对于问题1:由于MongoDB不是关系数据库,因此没有任何内置功能。您必须通过查询来删除博客的所有评论,为此,您必须相应地创建评论架构。

如果您正在使用猫鼬ODM,则可以尝试使用猫鼬中间件来减少预定义查询的工作量

https://mongoosejs.com/docs/middleware.html

但是您必须相应地管理模式,以便可以找到所有必需的注释

对于问题2:  试试看

<% for(var i=0; i<blog.comments.length; i++) {%> <p><strong><%=blog.comments[i].author%></strong>-<%=blog.comments[i].text%></p>> <% } %>

答案 1 :(得分:1)

您可以使用例如猫鼬的预钩子

blogSchema.pre('remove', function(next) {
    Comment.remove({author: this._id}).exec(); // Comment model
    next();
});

查看更多https://mongoosejs.com/docs/middleware.html#pre

这是代码https://github.com/arifmahmudrana/task-api/blob/master/src/models/User.js#L158-L168

的有效示例

这应该有效

const mongoose = require('mongoose'); // import mongoose
const Schema = mongoose.Schema; // extract schema for easy use

// declare blogSchema
const blogSchema = new Schema({
  title: String,
  image: String,
  body: { type: String, default: '' },
  created: { type: Date },
  comments: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Comment'
    }
  ]
});

const Blog = mongoose.model('Blog', blogSchema); // Blog model

const commentSchema = Schema({
  text: String,
  author: String
});
const Comment = mongoose.model('Comment', commentSchema); // Comment model
blogSchema.pre('remove', function(next) {
  Comment.remove({ author: this._id }).exec(); // Comment model
  next();
});

module.exports = { Blog, Comment };

// Now import Blog here
app.delete('/blogs/:id', function(req, res, next) {
  //destroy blog
  Blog.findByIdAndRemove(req.params.id, function(err) {
    if (err) {
      res.redirect('/blogs');
    } else {
      res.redirect('/blogs');
    }
  });
});