TypeError:更新文档时在非对象上调用Object.keys [英] TypeError: Object.keys called on non-object when updating document
本文介绍了TypeError:更新文档时在非对象上调用Object.keys的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过增加计数器并将对象推入数组来更新现有文档.
I'm trying to update an existing document by increment a counter and pushing an object to an array.
以下是用户的架构:
var UserSchema = new Schema({
[...]
posts: {
totalWords: { type: Number, min: 0, default: 0 },
_entries: [
{
words: { type: Number, min: 0 },
body: { type: String },
date: Date
}
]
},
});
这是更新代码:
var newPost = {
words: req.body.words,
body: req.body.entry,
date: new Date()
};
User.findOne(req.user._id, function (err, user) {
var previous = user.posts.totalWords;
user.posts.totalWords = previous + newPost.words;
user.posts._entries.push(newPost);
user.save(function (err) {
if (err) return res.send(400, err);
return res.json(newPost);
});
});
我收到以下错误:
[TypeError: Object.keys called on non-object]
关于如何解决此问题的任何想法?
Any ideas on how to solve this?
推荐答案
回答我自己的问题
我能够通过以下方式解决问题:
I was able to solve the problem by changing:
User.findOne(req.user._id, function (err, user) { [...] });
对此:
User.findById(req.user._id, function (err, user) { [...] });
这篇关于TypeError:更新文档时在非对象上调用Object.keys的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文