【问题标题】:Meteor.call and server methods not working properlyMeteor.call 和服务器方法无法正常工作
【发布时间】:2016-11-12 03:53:39
【问题描述】:

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

// 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

【问题讨论】:

  • 小提醒:我们需要代码在此处in 出现问题 - 粘贴板的链接本身可能会导致帖子被标记为离题。
  • 不确定你为什么首先使用findOne() 作为异步函数?
  • @halfer 抱歉,我会更新问题,说实话代码似乎很大
  • @ghybs 只需要来自另一个集合的信息来创建一个帐户,在这种情况下是一个来源:D
  • 不过,您可以对查询使用同步语法。你试过了吗?

标签: asynchronous meteor node-fibers


【解决方案1】:

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

您定义了“createAccount”,但在客户端您正在调用“createExternalAccount”

【讨论】:

  • 是的,没关系,我实际上改变了一些方法的名称,可能忘记了那个:) 抱歉
  • @maumercado:请将您的代码示例更新为最新代码,以免新读者得出相同的结论。谢谢。
猜你喜欢
  • 2015-10-27
  • 2012-01-20
  • 1970-01-01
  • 1970-01-01
  • 2015-03-06
  • 1970-01-01
  • 2013-03-22
  • 2017-06-02
  • 2015-10-27
相关资源
最近更新 更多