【发布时间】:2018-04-01 18:58:14
【问题描述】:
我编写了这个服务,当我调用它时,JSON 响应是组已更新,但是当我检查组详细信息时它没有更新,旧的详细信息仍然存在。我不知道问题出在哪里。
这是我的代码:
app.post('/api/updateGroup/:group_id', function(req, res) {
var checkGroup = Group.findOne({'_id': req.params.group_id }).exec();
checkGroup.addBack(function(err, existingGroup) {
if (err) {
res.json({'message': err });
} else if (existingGroup) {
var group = existingGroup;
Group.findOne({'_id': req.params.group_id })
.execQ()
.then(function(existingUser) {
var friendphoneNumber = req.body.friendphoneNumber.split(',');
var friends = [];
console.log('existingUser', friendphoneNumber);
async.each(friendphoneNumber, function(phonenum, callback) {
var phonenum = phonenum.split("\'")[0];
console.log('phonenum', phonenum);
User.findOne({'phoneNumber': phonenum })
.execQ()
.then(function(existingFriend) {
if (existingFriend === null) {
friends.push({'details': {'phoneNumber': phonenum } });
} else {
friends.push({'details': existingFriend });
}
})
.catch(function(err) {
console.log('err', err)
friends.push({'details': {'phoneNumber': phonenum } });
})
.done(function() {callback(); });
}, function(err) {
friends.push({'details': {'phoneNumber': friendphoneNumber } });
existingGroup.friends = friends;
existingGroup.save();
// existingGroup.update({ '_id': req.params.group_id},{ "$set": {'friends': req.body.friendphoneNumber} } , function(err) {
// existingGroup.update({'_id': req.params.group_id}, {update[[, 'friends': req.body.friendphoneNumber]}, callback]);
existingGroup.update(function(err) {
if (err) {
res.json({message: err });
} else {
res.json({success: 1, message: 'Group updated', updatedGroup: existingGroup });
}
});
});
})
.catch(function(err) {
res.json({success: 0, message: 'user id Not Match. Please try again'});
}).done(function(events) {});
} else {
callback();
}
});
});
【问题讨论】:
-
试试
existingGroup.save(function(err){if(err)console.error(err)})看看有没有错误 -
做了,不,它没有进入 err 循环,它涉及到 else,我可以看到更新的组详细信息作为 json 响应.. bt 在 db 中不一样 :(
-
或者可能只是显示现在已更新,实际上并没有在 db 中更新它...
-
你如何再次查询数据库以查看它是否更新?
-
用postman中的id查看组的详细信息
标签: node.js mongodb updates savechanges database