【问题标题】:Meteor template updates before result of Meteor.users.updateMeteor 模板在 Meteor.users.update 结果之前更新
【发布时间】:2014-09-06 13:22:13
【问题描述】:

我正试图弄清楚如何防止模板在 Meteor.users.update() 完成之前更新。

首先,我试图弄清documentation and the use of an optional callback argument 的含义,以便弄清楚发生了什么。

这是我所拥有的:

Meteor.users.update(Meteor.userId(),
                    {$set:{'profile.reviewList': []}},
                     [],
                      function(err, result){
                            if (err){
                              console.log('oh no!');
                            } else {
                            console.log('Result achieved: '+this.profile.reviewList);
                            }
                          });

目前console.log('Result achieved: '+this.profile.reviewList); 总是第一次返回类似...TypeError: Cannot read property 'reviewList' of undefined... 的东西,但它告诉我它在结果返回之前被触发。

我确定我没有正确实现回调,但我试图模拟这个答案:How do you ensure an update has finished in meteor before running a find?

我真的很想延迟关联模板的重新渲染,直到属性被创建。

任何帮助将不胜感激。

【问题讨论】:

    标签: meteor


    【解决方案1】:

    你假设回调函数中的作用域(this)返回user对象,这是错误的。

    如果您想在该回调中获取 user 对象,只需在该处查询即可:

    var user = Meteor.users.find(Meteor.userId()).fetch()
    

    另一件事,您将empty array 作为不需要的第二个参数传递。

    Meteor.users.update(
        Meteor.userId(), 
        {
            $set: {
                'profile.reviewList': 'testData'
            }
        },
        function(err, result) {
            if (err) {
                console.log('oh no!');
            } else {
                var user = Meteor.users.find(Meteor.userId()).fetch();
                console.log('Result achieved: ' , user && user.profile && user.profile.reviewList);
            }
        }
    );
    

    【讨论】:

    • 这行得通,谢谢。我不熟悉此行中使用的符号console.log('Result achieved: ' , user && user.profile && user.profile.reviewList); 我的猜测是控制台不会运行,直到所有这三个实体都存在。这是必要的吗? console.log 不会等到结果返回吗?或者更准确地说,只有在操作成功的情况下?
    • a && b 被称为守卫运算符。见explanation how it works
    猜你喜欢
    • 2015-05-03
    • 2015-03-05
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    • 2017-08-27
    • 2014-12-08
    • 2016-04-30
    相关资源
    最近更新 更多