Problem 1:我有一个带有对象数组的模型,一个对象包含多个对象,我想用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'
}]
});commentSchema
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只获取一个包含相关评论的博客。如果我用<%=blog.comments%>在show.ejs中编码,那么它将返回
{ 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)任何帮助都将不胜感激。很抱歉一次问了太多问题。耽误您时间,实在对不起。
提前谢谢。
发布于 2019-09-23 12:48:58
问题1的:由于MongoDB不是关系数据库,因此没有任何内置特性可供使用。您必须通过查询删除博客的所有评论,为此,您必须相应地创建注释模式。
如果您正在使用mongoose ODM,您可以尝试mongoose中间件来减少您对预定义查询的工作量。
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>> <% } %>发布于 2019-09-24 05:50:23
你可以用猫鼬的预钩
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');
}
});
});https://stackoverflow.com/questions/58061116
复制相似问题