我有一个看起来像这样的模型:
mongoose.Schema({
username: String,
posts: [{ type: Schema.Types.ObjectId, ref: 'Post' }]
});
Run Code Online (Sandbox Code Playgroud)
我有一个要传递 ObjectID 的端点:
app.delete('/post', function(req, res) {
User.findOne({ _id: req.user._id}, function(err, result) {
result.pull({ _id: req.body.post_id });
});
});
Run Code Online (Sandbox Code Playgroud)
感觉它应该可以工作,但我收到此错误:
CastError: Cast to ObjectId failed for value "[object Object]"
我究竟做错了什么?
如果您想从数组中删除一个元素,请使用此
User
.update(
{_id: req.user._id},
{ $pull: {posts: req.body.post_id } }
)
.then( err => {
...
});
Run Code Online (Sandbox Code Playgroud)
这里的文档
| 归档时间: |
|
| 查看次数: |
8854 次 |
| 最近记录: |