I have a model that looks like this:
mongoose.Schema({
username: String,
posts: [{ type: Schema.Types.ObjectId, ref: 'Post' }]
});
I have an endpoint that I want to pass an ObjectID:
app.delete('/post', function(req, res) {
User.findOne({ _id: req.user._id}, function(err, result) {
result.pull({ _id: req.body.post_id });
});
});
Feels like it should work, but I'm getting this error:
CastError: Cast to ObjectId failed for value "[object Object]"
What am I doing wrong?
If you want remove one element from an array use this
User
.update(
{_id: req.user._id},
{ $pull: {posts: req.body.post_id } }
)
.then( err => {
...
});
Here the documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With