Meteor.call和服务器方法无法正常工作

时间:2016-07-09 20:14:31

标签: asynchronous meteor node-fibers

我的流星应用程序上有这个代码:

// client side
Template.lead.events({
    'submit .insertExternalAccountForm': function (event) {
        event.preventDefault();
        Session.set('mcsStatus', 'Creating external account ...');

        var target = {
            code: event.target.code.value,
            leadId: event.target.leadId.value,
            name: event.target.name.value,
            username: event.target.username.value,
            password: event.target.password.value,
            searchSourceId: event.target.searchSourceId.value,
            clientId: event.target.clientId.value,
            clientUserId: event.target.clientUserId.value
        };

        var noFormError = true;
        if (target.username.length === 0) {
            Session.set("username_error", "Field must not be empty");
            noFormError = false;
        } else {
            Session.set("username_error", null);
        }

        if (target.password.length === 0) {
            Session.set("password_error", "password must not be empty");
            noFormError = false;
        } else {
            Session.set("password_error", null);
        }

        if (!noFormError) {
            return noFormError;
        }

        Meteor.call('createExternalAccount', target, function (err, res) {
            if (err) {
                console.error(err);
            }
            console.log('in meteor call');
            Router.go('/' + res.domain + '/' + res.externalId);
        });
    }
});

//server side

var createExternalAccountSync = function (query, external) {
    return models.SearchSources.findOne(query).exec()
    .then(function (searchsource) {
        external.domain = searchsource.source;
        var emr = searchsource.source.split('-');
        return models.Organization.findOne({externalId: emr[2]}).exec();
    }).then(function (org) {
        console.log('after org');
        external.organizationId = org._id;
        return models.AppUser.findOne({clientId: external.clientId, externalId: external.clientUserId }).exec();
    }).then(function (user) {
        console.log('after app user');
        external.userId = user._id;
        external.userIds = [user._id];
        return new Promise(function (resolve,reject) {
            console.log('saveOrUpdate');
            models.ExternalAccount.saveOrUpdate(external, function (err, newE) {
                if (err) {
                    console.error(err)
                    reject(err);
                }
                resolve(newE)
            });
        });
    })
    .catch(function (e) {
        console.error(e);
        throw new Meteor.Error(e);
    });
};


Meteor.methods({'createExternalAccount': function (data) {
        var query = {};
        var newExternalAccount = new models.ExternalAccount();

        newExternalAccount.username = data.username;
        newExternalAccount.password = data.password;
        newExternalAccount.externalId = data.username;
        newExternalAccount.name = data.name;
        newExternalAccount.clientId = data.clientId;
        newExternalAccount.clientUserId = data.clientUserId;
        newExternalAccount._metadata = { leadId: data.leadId };
        if (data.code === 'f') {
            query.searchSourceId = '5744f0925db77e3e42136924';
        } else {
            query.searchSourceId = data.searchSourceId;
        }

        newExternalAccount.searchSourceId = query.searchSourceId;
        console.log('creating external account')
        createExternalAccountSync(query, newExternalAccount)
        .then(function (external) {
            console.log('should return to meteor call');
            return external;
        })
        .catch(function (e) {
            console.error(e);
            throw new Meteor.Error(e);
        });
    }
});

我遇到的问题是服务器端的代码在被正确调用的情况下没有触发客户端meteor.call,没有console.log输出或任何东西。我相信Meteor.wrapAsync方法已正确使用,但仍然没有在客户端显示任何内容,实际上并没有重定向到我希望用户在表单提交后去的地方。

更新

代码已更新为最新版本,但现在我在客户端上遇到了一个奇怪的错误,实际上因为模板上的meteor.call方法既没有返回错误也没有结果

Exception in delivering result of invoking 'createExternalAccount': http://localhost:3000/app/app.js?hash=c61e16cef6474ef12f0289b3f8662d8a83a184ab:540:40
http://localhost:3000/packages/meteor.js?hash=ae8b8affa9680bf9720bd8f7fa112f13a62f71c3:1105:27
_maybeInvokeCallback@http://localhost:3000/packages/ddp-client.js?hash=27502404fad7fc072e57e8b0b6719f40d92709c7:3557:21
receiveResult@http://localhost:3000/packages/ddp-client.js?hash=27502404fad7fc072e57e8b0b6719f40d92709c7:3577:30
_livedata_result@http://localhost:3000/packages/ddp-client.js?hash=27502404fad7fc072e57e8b0b6719f40d92709c7:4742:22
onMessage@http://localhost:3000/packages/ddp-client.js?hash=27502404fad7fc072e57e8b0b6719f40d92709c7:3385:28
http://localhost:3000/packages/ddp-client.js?hash=27502404fad7fc072e57e8b0b6719f40d92709c7:2736:19
forEach@[native code]
forEach@http://localhost:3000/packages/underscore.js?hash=27b3d669b418de8577518760446467e6ff429b1e:149:18
onmessage@http://localhost:3000/packages/ddp-client.js?hash=27502404fad7fc072e57e8b0b6719f40d92709c7:2735:15
dispatchEvent@http://localhost:3000/packages/ddp-client.js?hash=27502404fad7fc072e57e8b0b6719f40d92709c7:175:27
_dispatchMessage@http://localhost:3000/packages/ddp-client.js?hash=27502404fad7fc072e57e8b0b6719f40d92709c7:1160:23
_didMessage@http://localhost:3000/packages/ddp-client.js?hash=27502404fad7fc072e57e8b0b6719f40d92709c7:1218:34
onmessage@http://localhost:3000/packages/ddp-client.js?hash=27502404fad7fc072e57e8b0b6719f40d92709c7:1365:28

1 个答案:

答案 0 :(得分:0)

根据您提供的代码,可能是因为您正在调用不同的方法。

您定义了' createAccount'但是在客户端,您正在呼叫' createExternalAccount'