【发布时间】:2018-01-14 03:09:50
【问题描述】:
所以我有这个 API 方法:
router.delete('/circleAlertDelete/:id', function(req, res, next){
User.findOne({_id: req.params.id}).then(function(user){
var userTemp = user;
var alerts = user.circleAlerts;
var i = 0;
for(i=0; i<alerts.length; i++){
if(alerts._id == req.body.alertID){
alerts.splice(i,1);
}
}
userTemp.circleAlerts = alerts;
console.log(req.params.id);
User.findByIdAndUpdate({'_id': req.params.id}, userTemp, function(err, user){
console.log("error:",err);
res.send(user);
});
}).catch((err) => console.error(err));
});
我也试过了,没用:
{'_id': mongo.ObjectId(req.params.id)}
当运行时打印console.log(req.params.id);的id是数据库中一个用户的id。
但也打印的是error: null。
想不通,谢谢 Ed。
【问题讨论】:
-
那么它是如何失败的呢?有错误吗?
-
它通过但找不到用户,因此不更新它。
-
您没有用
if(err)语句包围console.log('error:', err)。这样,错误行总是被打印出来。即使没有错误。 -
是的,没有错误...好点...
标签: javascript node.js mongoose