【发布时间】:2019-05-30 07:13:18
【问题描述】:
$.post($gameNetwork._serverURL+'/addfriend',
{username:"r",tusername:"w"}).done(function (data) {
console.log("finished");
});
Account.statics.
friend = function(name,tname,cb) {
return this.findOneAndUpdate(
{ 'username': name },
{ $push: {'friendlist': tname}},
{ upsert: true, new: true},
cb);
};
路线
router.post('/addfriend', function(req, res) {
//Account.findByName(req.body.username, function(err, account){
Account.friend(req.body.username,req.body.tusername, function(err, account){
if (err) {
return res.status(203).json({
err: err.msg
});}
if (!account) {
return res.status(203).json({
err: "Invalid username"
});}
var tname = req.body.tusername;
var profile = {
tname : tname,
name: account.username,
email: account.email,
id: account._id,
rank: account.rank
}; });
这段代码应该在 Mongodb 的“friendlist”字段中输入“w”,但我得到的是 null 而不是 w。
如何在 Mongodb 的“好友列表”字段中输入“w”。 任何帮助表示赞赏 提前致谢
【问题讨论】:
-
第一步总是做一些基本的调试。如果你添加
console.log(req.body);,你能看到你期望看到的吗? -
我重启了服务器和浏览器,一切正常
-
感谢所有cmets
标签: javascript node.js http-post