Javascript 无法更新$push-in-mongoose、nodejs
我试图在我的博客中添加注释选项,但在使用update(),$push在模式中更新它时,我得到了注释选项。我做错了什么 posts.js->routes posts.js->models index.jadeJavascript 无法更新$push-in-mongoose、nodejs,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,我试图在我的博客中添加注释选项,但在使用update(),$push在模式中更新它时,我得到了注释选项。我做错了什么 posts.js->routes posts.js->models index.jade if post.comments h3 Comments each comment, i in post.comments
if post.comments
h3 Comments
each comment, i in post.comments
p.comment-name #{comment.name}
p.comment-text #{comment.body}
输出
帖子:57fgc24d24d5f3hj,评论:“这是一条评论”
我尝试了以下问题,但无法理解。
以及如何添加功能以在评论中添加评论。谢谢对于
.update()
方法,{new:true,save:true}
选项实际上无效。看起来你误解了一些人的旧博文,他们实际上使用了.findOneAndUpdate()
或类似的内容。这与这里的任何问题都没有关系。我通常会怀疑var errors=req.validationErrors();
并没有返回您认为它会返回的内容。因此,事实上,您可能只是一直在分支到只执行findById()
的代码。但是,db.collection()
调用对mongoose
无效或是必需的,并且您似乎在“覆盖”模型。我添加了{new:true,save:true}
在阅读了这里的答案之后,甚至在添加之前,我都无法更新。我正在遵循一个教程(是的,已经有2年了),他们在那里使用了db.get()
,.update()
,`$push()。`他们甚至没有使用模式。在跟踪文档、博客和一些问题之后,我在这里添加了模式和db.collection()。在我的博客中添加帖子时,我做了同样的事情,但是使用了.insert()
,并且它正在工作,所以我认为var错误
没有任何问题。虽然我删除了这部分,但问题仍然是一样的。现在我删除了db.collection()但你能解释一下为什么我必须在插入时添加db.collection,而不是在更新时添加吗?谢谢。
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var lists = new Schema({
title:{
type: String,
required: true
},
author_username:{
type: String
},
author_name: {
type: String
},
category:{
type: String
},
body:{
type: String,
required: true
},
// comments: [CommentsSchema]
comments: [{
name:{
type: String,
},
body:{
type: String,
},
parent: {
type: String
},
commentdate :{
type: Date,
default: Date.now
}
}],
date:{
type: Date,
default: Date.now
}
});
var posts = module.exports = mongoose.model('posts', lists);
if post.comments
h3 Comments
each comment, i in post.comments
p.comment-name #{comment.name}
p.comment-text #{comment.body}