不包括所有其他工作,但工作正常。我有一个工作的server.js用户服务和控制器来注册和存储我的mongoDB数据库中的用户。任何人都知道如何存储得分变量并在每次用户正确时更新数字吗? 我的尝试如下。
dashboard.component.ts
export class DashboardComponent{
makeMatch() {
if (this.imageMatch === this.emotionMatch) {
this.score= this.score+ 1;
}
user.service.js
function _score(_id) {
Users.findById(id, function (err, user) {
if (err) return handleError(err);
user.score += 1;
user.save(function(err) {
if (err) return handleError(err);
res.send(user); // Or redirect, basically finish request.
});
});
}
答案 0 :(得分:0)
请尝试这样的事情:
function _score(userId) {
Users.findByIdAndUpdate(userId, {$inc: {score: 1}},
function (err, data) {
// here you can handle errors and redirect or send data back to the user...
});
}
在前端,您需要拥有当前登录用户的ID,以便您可以使用userId作为参数从上面调用方法。我认为你正在寻找的是这种情况。 希望它有所帮助!