maumercado
maumercado

Reputation: 1473

Meteor.call and server methods not working properly

I have this code on my meteor app:

// 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);
        });
    }
});

The problem that I'm having is that the code on the server side, while it's being called properly, is not triggering the client side meteor.call, there's no console.log output or anything. I believe that the Meteor.wrapAsync method is properly used, but still not showing anything on the client side, and not in fact redirecting where I want the user to go after form submission.

UPDATE

The code has being updated to the newest form, but now I'm getting a weird error on the client, and its actually because the meteor.call method on the template returns neither error or result

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

Upvotes: 0

Views: 544

Answers (1)

jan mraz
jan mraz

Reputation: 57

By the code you provided,it could be because you are calling different method.

You defined 'createAccount' but on client side you are calling 'createExternalAccount'

Upvotes: 0

Related Questions