2015-10-04 5 views
0

У меня проблема: если я попытаюсь обновить документ ObjectId, я получаю файл MongooseError.CastError. Любая помощь?Mongoose, обновляющий документ ObjectId

var comment = new Comment({ 
    contents: 'contents' 
}) 

console.log(typeof req.body.postId) // 'string' 

Post.update({_id: db.Types.ObjectId(req.body.postId)}, { // 'cast error' 
    commentsId: {$push: comment._id} 
}, function(err, numAffected, res){ 
    if (err) { return next(err)} 
    console.log('success') 
}) 
+0

Вы также можете использовать метод 'findByIdAndUpdate' –

ответ

1

Позвольте Mongoose сделать кастинг для вас на основе определенной схемы.

Post.update({_id: req.body.postId}, {commentsId: {$push: comment._id}}, ... 
Смежные вопросы