0

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
maumercado
  • 1,453
  • 4
  • 23
  • 47
  • Small reminder: we need code to appear _in_ questions here - links to pasteboards on their own can cause the post to be marked as off-topic. – halfer Jul 09 '16 at 21:53
  • Not sure exactly why you use [`findOne()`](http://docs.meteor.com/api/collections.html#Mongo-Collection-findOne) as an async function in the first place? – ghybs Jul 10 '16 at 03:16
  • @halfer sorry about that I will update the question, the code seem to large to be honest – maumercado Jul 10 '16 at 06:23
  • @ghybs just need informatin from another collection in order to create an account, in this case a source :D – maumercado Jul 10 '16 at 06:24
  • 1
    Still, you can use synchronous syntax for your query. Have you tried it? – MasterAM Jul 10 '16 at 06:41
  • @maumercado Let me rephrase then: `var searchsource = collection.findOne(query)` – ghybs Jul 10 '16 at 13:37
  • Your new code still looks overly complicated for what you seem to try to achieve?.. BTW, your new method does not seem to return anything? – ghybs Jul 16 '16 at 22:40
  • @ghybs yeah the new method still returns undefined on meteor.call, however a console.log(external) before return external in the server side method does show the external object, and then the console.log on the meteor.call method pop up being undefined still – maumercado Jul 18 '16 at 16:36
  • You never return anything from your method. Please Add a return to return your createExternalAccountSync and you don't handle the error properly on client side. – user3636214 Jan 03 '18 at 21:54

1 Answers1

0

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'

jan mraz
  • 57
  • 2
  • yeah thats fine, I actually changed the names of some methods around and probably forgot about that one :) sorry – maumercado Jul 10 '16 at 06:22
  • 1
    @maumercado: please update your code example to the latest code, so that new readers will not arrive at the same conclusion. Thanks. – halfer Jul 10 '16 at 08:39