【问题标题】:how to work with callback when calling Meteor methods?调用 Meteor 方法时如何使用回调?
【发布时间】:2015-12-13 04:25:25
【问题描述】:

我注意到尽管服务器方法运行良好,但回调函数从未被执行。同样从 Meteor 文档中,我了解到,当 Meteor.Error 被抛出时,它会通知客户端,但我看不到它也能正常工作。我做错了什么?

客户

    if (Meteor.isCordova) {
 getContacts(function (contacts) {
            $meteor.call('createContacts', contacts, function(err){
               alert("in create contacts callback");
                if(err && err.error === "not-logged-in"){
                   alert("error due to not-logged-in");
                    $ionicPopup.alert({
                        title: err.reason || "User not logged in",
                        template: 'Please try again after logged in',
                        okType: 'button-positive button-clear'
                    });
                }
                else if(err && err.error === "contacts-exists"){
                    $ionicPopup.alert({
                        title: err.reason || "Connections already exists",
                        template: 'Please try again after logged in',
                        okType: 'button-positive button-clear'
                    });
                }
                $meteor.call('createConnections');
            });
});

}

function getContacts(success, error) {
    function onSuccess(contacts) {
        success && success(contacts);
    };

    var options = {};
    options.multiple = true;
    var fields = ["displayName", "name"];
    navigator.contacts.find(fields, onSuccess, error, options);

}

服务器

createContacts: function (contacts, callback) {
    if (!this.userId) {
        throw new Meteor.Error('not-logged-in',
            'Must be logged in to  update contacts')
    }
    var userId = this.userId, exist = Contacts.findOne({userId: userId});
    log.debug("Is contacts for userId %s exist in database ? %s", userId, !! exist);
    if (!exist) {
        Contacts.insert({'userId': userId, 'contacts': contacts}, function () {
            callback && callback();
        });
    } else {
        log.debug("Contacts for user exists so throwing exception as contacts-exists");
        var meteorErr =  new Meteor.Error('contacts-exists', "Contacts are already exist");
        callback && callback(meteorErr);
    }


},

【问题讨论】:

    标签: meteor


    【解决方案1】:

    这些回调是异步的。您的服务器端方法不应该调用回调函数,甚至也不应该期望一个作为参数。

    将回调作为最后一个参数传递给Meteor.call('createContacts') 是正确的,但由createContacts接收者决定何时调用该回调。简单来说,从客户端的角度来看,服务器的工作就是返回一个“OK”或“错误”信号。

    删除方法定义(在服务器上)中对回调的任何引用,并期望客户端在服务器响应时执行该回调。

    试试这个:

    服务器

    createContacts: function (contacts) {
        if (!this.userId) {
            throw new Meteor.Error('not-logged-in',
                'Must be logged in to  update contacts');
        }
        var userId = this.userId;
        var exist = Contacts.findOne({userId: userId});
        log.debug("Is contacts for userId %s exist in database ? %s", userId, !! exist);
        if (!exist) {
            Contacts.insert({'userId': userId, 'contacts': contacts});
        } else {
            log.debug("Contacts for user exists so throwing exception as contacts-exists");
            throw new Meteor.Error('contacts-exists', "Contacts are already exist");
        }
    },
    

    客户

       $meteor.call('createContacts', contacts, function(err){
           alert("in create contacts callback");
            if(err && err.error === "not-logged-in"){
               alert("error due to not-logged-in");
                $ionicPopup.alert({
                    title: err.reason || "User not logged in",
                    template: 'Please try again after logged in',
                    okType: 'button-positive button-clear'
                });
            }
            else if(err && err.error === "contacts-exists"){
                $ionicPopup.alert({
                    title: err.reason || "Connections already exists",
                    template: 'Please try again after logged in',
                    okType: 'button-positive button-clear'
                });
            }
            $meteor.call('createConnections');
        });
    

    【讨论】:

    • 我仍然没有成功。我已经更新了我正在尝试的客户端代码。
    • 在您更新的代码中,服务器端方法仍然期望并确定是否调用回调。请记住,这不是服务器的工作。
    • 我使用的是你的服务器代码,但我只更新了我正在尝试的客户端代码。
    • 我在getContacts的回调函数中使用Meteor.call。
    【解决方案2】:

    似乎我需要使用.then() 中记录的Angular-Meteor Document

    还按照amageddian修改了代码

    【讨论】:

      猜你喜欢
      • 2013-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      • 1970-01-01
      • 2017-01-21
      • 2015-03-06
      相关资源
      最近更新 更多