简体   繁体   中英

Why is Meteor.call() not recognized?

This question has morphed enough from its ancestor that it was suggested I post a new question.

When I call my " insertPerson " method, which then calls my " getTextAddrAsEmailAddr " method, I see, in the console, my two debug console.log msgs:

I20151022-07:59:07.240(-7)? insertPerson reached
I20151022-07:59:07.365(-7)? phone is 0871632810

...followed by this exception:

I20151022-07:59:07.365(-7)? Exception while invoking method 'insertPerson' TypeE
rror: Cannot call method 'call' of undefined
I20151022-07:59:07.365(-7)?     at [object Object].Meteor.methods.getTextAddrAsE
mailAddr (both/methods.js:37:28)

NOTE: Line 37 in methods.js is:

return Meteor.http.call("GET", restcall);

For context, the entire methods.js file is:

Meteor.methods({
    'insertPerson': function(firstname, lastname, streetaddr1, streetaddr2, placename, stateorprov, zipcode, emailaddr, phone, notes) {
        console.log('insertPerson reached'); // TODO: Remove before deploying
        check(firstname, String);
        . . .
        console.log('phone is ' + phone);
        var textAddrAsEmailAddr = Meteor.call('getTextAddrAsEmailAddr', phone);
        console.log('textAddrAsEmailAddr is ' + textAddrAsEmailAddr);

        People.insert({
            per_firstname: firstname,
            per_lastname: lastname,
            per_streetaddr1: streetaddr1,
            per_streetaddr2: streetaddr2,
            per_placename: placename,
            per_stateorprov: stateorprov,
            per_zipcode: zipcode,
            per_emailaddr: emailaddr,
            per_phone: phone,
            per_textaddrasemailaddr: phone,
            per_notes: notes,
            per_createdBy: this.userId
        });
    },
    'getTextAddrAsEmailAddr': function(phone) {
        this.unblock();
        var restcall = 'http://www.reminder.com/phone.check.php?number=' + phone;
        return Meteor.http.call("GET", restcall);
    }
});

Is something wrong with my Meteor.http.call() call?

It should be HTTP.call() not Meteor.http.call() according to the docs .

(Also make sure you've added the package with meteor add http ).

Ok, it is not written as a hard fact inside the docs but I am pretty sure, that Meteor.call() is expected to make a remote call from client to server.

In addition, I am wondering, why you would make a Meteor.call() at this point, when you are already "on server side".

Try:

function getTextAddrAsEmailAddr(scope, phone) {
    scope.unblock();
    var restcall = 'http://www.reminder.com/phone.check.php?number=' + phone;
    return HTTP.get(restcall);
}

Meteor.methods({
  'insertPerson': function(firstname, lastname, streetaddr1, streetaddr2, placename, stateorprov, zipcode, emailaddr, phone, notes) {
     console.log('insertPerson reached'); // TODO: Remove before deploying
     check(firstname, String);
     . . .
     console.log('phone is ' + phone);
     var textAddrAsEmailAddr = getTextAddrAsEmailAddr(this, phone);
     console.log('textAddrAsEmailAddr is ' + textAddrAsEmailAddr);

     People.insert({
        per_firstname: firstname,
        per_lastname: lastname,
        per_streetaddr1: streetaddr1,
        per_streetaddr2: streetaddr2,
        per_placename: placename,
        per_stateorprov: stateorprov,
        per_zipcode: zipcode,
        per_emailaddr: emailaddr,
        per_phone: phone,
        per_textaddrasemailaddr: phone,
        per_notes: notes,
        per_createdBy: this.userId
    });

    return true;
  }
});

If you want to have the function getTextAddrAsEmailAddr available as a Meteor method as well, just add:

'getTextAddrAsEmailAddr': function(phone) {
    return getTextAddrAsEmailAddr(this, phone);
}

Hope that fixes it for you

Cheers Tom


Update:

I was wondering myself about the overhead and correctness about using Meteor.call()

  1. You may use it everywhere and anywhere

  2. Be aware of the overhead

Meteor.call is defined in:

Package ddp-client/livedata_connection.js L665

and will run via Meteor.apply all the time which is defined at:

Package ddp-client/livedata_connection.js L706

If you check that source from L707-L912 , I guess it is not wondering, that the suggestion to call the function directly is much more efficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM