mongoose使用model.update更新文档中的值

时间:2022-12-02 02:34:22

What am i doing wrong here, I want to update the score?

我在这里做错了什么,我想更新分数?

the problem is it executes the code without errors but doesn't update the score.

问题是它执行代码没有错误但不更新分数。

Team.findOne({name: req.body.team}, function(err, teamData){            
    if(teamData) {
        var a = teamData.score + 1;
        Team.update({name: req.body.team},{$set: {score : a}});
    }
    else {
        console.log(err);
    }
});

1 个解决方案

#1


0  

As Muhammad Ali said, update requires a callback function, that is probably your problem. But there is also an easier way to do this:

正如*·阿里所说,更新需要一个回调函数,这可能是你的问题。但是还有一种更简单的方法:

Team.findOne({name: req.body.team}, function(err, teamData){            
    if(teamData){
        teamData.score += 1
        teamData.save(function(err) {
            if (err) // do something
        });
    }else{
        console.log(err);
    }
});

#1


0  

As Muhammad Ali said, update requires a callback function, that is probably your problem. But there is also an easier way to do this:

正如*·阿里所说,更新需要一个回调函数,这可能是你的问题。但是还有一种更简单的方法:

Team.findOne({name: req.body.team}, function(err, teamData){            
    if(teamData){
        teamData.score += 1
        teamData.save(function(err) {
            if (err) // do something
        });
    }else{
        console.log(err);
    }
});