【发布时间】:2015-11-10 15:31:50
【问题描述】:
当我将新评论文档推送到 Mongoose 的故事集中时,我不断收到以下错误:
{
[MongoError: The field 'comments' must be an array but is of type Object in document {_id: ObjectId('55d2429477da83b6f593ce53')}]
name: 'MongoError',
message: 'The field \'comments\' must be an array but is of type Object in document {_id: ObjectId(\'55d2429477da83b6f593ce53\')}',
driver: true,
index: 0,
code: 16837,
errmsg: 'The field \'comments\' must be an array but is of type Object in document {_id: ObjectId(\'55d2429477da83b6f593ce53\')}'
}
这是我的模型:
var commentSchema = new Schema({
text: String,
date: { type: Date, default: Date.now },
author: String
})
var storySchema = new Schema({
title: String,
link: String,
comments: [commentSchema],
author: String,
date: { type: Date, default: Date.now }
});
这是我的查询:
app.post('/news/:id/comment', function(req, res) {
console.log('Hi Received');
var comment = {
"author": 'Steven',
"text": req.body.comment
}
Story.findOne({_id: req.params.id}, function(err, data) {
if(err) throw err;
data.comments.push(comment);
data.save(function(err, data){
if (err) console.log(err);
console.log(data);
});
})
});
我已经尝试用谷歌搜索解决方案,但仍然无法修复错误。
编辑:
我要添加的集合目前如下所示:
> db.stories.find().pretty()
{
"_id" : ObjectId("55d2429477da83b6f593ce53"),
"author" : "Steven Chen",
"link" : "AS",
"title" : "AS",
"date" : ISODate("2015-08-17T20:22:44.271Z"),
"comments" : {
"author" : "Steven",
"text" : "Alsawdksada"
},
"__v" : 0
}
【问题讨论】:
-
您的收藏文档与您的架构不匹配。正如错误消息所说,
comments是一个对象,而不是一个数组。
标签: javascript node.js mongodb mongoose nosql