Node.js 使用mongoose更新文档
我有个小问题。通过这段代码,我使用node.js和mongoose更新了mongodb中的现有文档Node.js 使用mongoose更新文档,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我有个小问题。通过这段代码,我使用node.js和mongoose更新了mongodb中的现有文档 router.route('/profile/:profile_id/:session_key') .put(function(req, res) { if (req._body == true && req.is('application/json') == 'application/json' ) { // Get the pro
router.route('/profile/:profile_id/:session_key')
.put(function(req, res) {
if (req._body == true && req.is('application/json') == 'application/json' ) {
// Get the profile to update
Profile.findOne({ profile_id: req.params.profile_id }, function(err, t_profile) {
if (err) {
res.send(err);
} else {
if(!t_profile) {
res.json({ status: 'CANT FIND PROFILE TO UPDATE' });
} else {
if (t_profile.session_key == req.params.session_key) {
// Update profile with new data
t_profile.name = setReq( t_profile.name, req.body.name );
t_profile.sex = setReq( t_profile.sex, req.body.sex );
t_profile.birthdate = setReq( t_profile.birthdate, req.body.birthdate );
t_profile.country = setReq( t_profile.country, req.body.country );
t_profile.password = setReq( t_profile.password, req.body.password );
// save updatd profile
t_profile.save( { _id: profile._id }, function(save_err, u_profile) {
if (save_err) {
res.json({ status: 'DB ERROR' });
} else {
res.json({ status: 'OK' });
}
});
} else {
res.json({ status: 'NOT LOGGED IN' });
}
//res.json({ status: "THIS RUNS"});
}
}
});
} else {
res.json({ status: 'ERROR', msg: 'Not application/json type'});
}
});
但是我的函数作为参数插入到.save()函数neve runs中,我需要取消对res.json行的注释({status:“THIS runs”});将som响应返回到客户端。为什么不是res.json({status:'DB ERROR'});或res.json({status:'OK'});发回?愚蠢的错误,t\u profile.save(函数(save\u err,u\u profile){…}修复了它