【问题标题】:MeteorJs, unable to get method.call return value outside of the methodMeteorJs,无法在方法之外获取method.call返回值
【发布时间】:2015-07-06 20:23:15
【问题描述】:

我有以下自动生成钩子代码。如何在 method.call 之外获得价值。

我的问题是,当我运行 method.call 时,'chi' 值未定义。然而,在服务器上有 '1' 记录。但是 chi 没有得到 'myResult' 值。如果我注释掉method.call并返回'Gogo',那么'chi'会正确获取这个值。有人可以指导我做错了什么以及如何纠正它。

代码:

    before: {
    method: function(doc) {
        var retVal = false ;
        var pai = Q.fcall(function(){
            if(!_.isEmpty(doc) && _.pick(doc, 'name') ) {
                console.log('Ist level, true condition: ', doc);
                return true;
            }
            else{
                console.log('Ist level, false condition: ', doc);
                return false;
            }
        })
            .then(function(check){
                console.log('Check value: ', check);

                if( check ){
                 Meteor.call('CategoryNameAvailable', doc.name, function (error, result) {

                        console.log('Returned result from server', result);
                        if (!result) {
                            if(Contexts.Category.keyIsInvalid('name')){
                                Contexts.Category.resetValidation('name');
                            }
                            console.log('Returned result from server inside if condition  ', result);
                            Collections.Category.simpleSchema().namedContext("CategoryInsertForm").addInvalidKeys([{
                                name: "name",
                                type: "notUnique"
                            }]);

                            console.log('Doc value in meteor call function: ', doc);
                            Session.set('retVal', true);
                            console.log('retVal value in meteor call function: ', retVal);
                        }
                        return 'myResult';
                    });
                    // return 'Gogo';
                    /*  Meteor call End  */
                }

            })
            .then(function(chi){
                console.log('Chi value: ', chi);
            })
            .done();

        console.log('Pai value-2: ', pai);

    }  /* End of  method */
} /* End of 'before' hook */

【问题讨论】:

    标签: javascript meteor meteor-autoform


    【解决方案1】:

    你可以看看https://github.com/stubailo/meteor-reactive-method

    或许能解决你的问题

    【讨论】:

      【解决方案2】:

      您认为可以在定义方法的文件中添加吗?我最近尝试做类似的事情时遇到了类似的问题,这与我的方法定义的格式有关。

      对我来说,我在方法定义中返回数据的地方放错了地方。在另一个类似问题的另一个实例中,我没有在客户端订阅 Collection。

      如果这不是问题,并且您的调用正确返回数据,它只有没有将它传递到调用上下文之外,您可以尝试使用 Session.set 来定义会话变量然后可以在需要数据时调用。

      虽然没有方法定义的上下文,但很难准确判断发生了什么。

      【讨论】:

      • 问题是'if'条件在方法从服务器返回值之前完成它的执行。所以一些速度问题:) 'if' 比 method.call 更快。
      猜你喜欢
      • 2019-03-07
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      • 2012-06-23
      • 2020-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多