【问题标题】:Database update not working inside Meteor method数据库更新在 Meteor 方法中不起作用
【发布时间】:2015-11-04 17:46:45
【问题描述】:

我有一个模板事件,允许用户将内容插入 Meteor 数据库的集合中。

var challenge_info = {
        "creator_id": Meteor.userId(),
        "creator_firstname": creat_firstname,
        "creator_avatarurl": Session.get('chUser').avatar_url,
        "recepient_id": recepient_id,
        "recepient_firstname": recepient_firstname,
        "recepient_avatarurl": recepient_avatarurl,
        ...
        "state": 0
    };

    Meteor.call('insertChallenge', challenge_info);

    Meteor.call('incrementStatistic', Meteor.userId(),'c_sent');
    Meteor.call('incrementStatistic', recepient_id,'c_pending');

    Session.set('new_challenge_reset', true);

    Router.go('home');

调用方法“insertChallenge”时一切正常,它只调用 ChallengesList.insert(challenge_info)。

但是当尝试调用“incrementStatistic”方法时,什么也没有发生。终端没有给我任何错误,并且我无法在浏览器控制台上测试查询(在 Meteor 中不再可能在没有选择 ID 的情况下从客户端更新数据库,并且我已删除自动发布)。

这里是方法(在 Meteor.methods() 中):

incrementStatistic : function(user_id, stat){
    UserStatistics.update({"user_id": user_id}, {"$inc" : {stat: 1}});
},
decrementStatistic : function(user_id, stat){
    UserStatistics.update({"user_id": user_id}, {"$inc" : {stat: 1}});
},
createStatistics : function(){
    UserStatistics.insert({'user_id': Meteor.userId(), 'c_won': 0, 'c_lost': 0, 'c_accepted': 0 , 'c_rejected': 0, 'c_pending': 0, 'c_sent': 0});
    return true;
}

我可以使用UserStatistics.findOne({"user_id": Meteor.userId()}); 检索集合信息(在另一个模板中),但增量永远不会起作用。

虽然它可能与这个问题无关,但我也有一个使用 Collection.update() 的服务器方法,它工作得很好(虽然它使用_id 作为选择器):

updateChallengeState: function(challenge_id, new_challenge_state){
    ChallengesList.update({_id: challenge_id}, {"$set" : {"state": new_challenge_state}});
}

有人对可能发生的事情有任何线索吗?

【问题讨论】:

  • 你确定在更新前调用了服务器方法并且userstatistics中的用户记录存在?
  • 究竟是什么不起作用?如果您可以在不同的模板中检索用户数据并且数据是正确的,那么那些incrementStatistic 调用正在更新数据库。您是否遇到模板未更新的问题?也许您还需要显示您的模板 html 和帮助程序。
  • @Sasikanth,是的。在创建新帐户时,我使用“createStatistics”方法创建记录,并且记录存在并得到很好的填充。
  • @MichelFloyd,我猜是 server.js 上的“incrementStatistic”方法中的 UserStatistics.update() 不起作用。另一个模板能够从集合中获取信息,但 icrement 不起作用(它为我提供了使用“createStatistics”方法创建的对象),然后使用创建的助手在模板中显示它(基本上是 UserStatistics.find ())。没有其他代码干扰我上面的解释。
  • stat 字段是否由服务器发布?您是否在 mongo 控制台中查看过数据是否正在写入?

标签: meteor


【解决方案1】:

您似乎没有定义在这些方法中传递的user_id 变量(incrementStatisticdecrementStatistic)。

相反,就像您在createStatistics 方法中所做的那样,只需传递Meteor.userId(),我相信您也不需要发送stat

incrementStatistic : function(){
    UserStatistics.update({user_id: Meteor.userId()}, {$inc : {stat: 1}});
},
decrementStatistic : function(){
    UserStatistics.update({user_id: Meteor.userId()}, {$inc : {stat: 1}});
},

并调用不带任何参数的方法:

Meteor.call('incrementStatistic');

顺便说一句,我建议你采用一种使用 mongodb 语法的方式。有时您似乎使用撇号 ('user_id'),有时使用引号 ("user_id"),有时两者都不使用 (user_id)。对于字段名称,使用它们是可选的,但对于运算符($inc、$set、$and、$or 等),不使用引号或撇号是标准的。参考the documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    相关资源
    最近更新 更多