Why is Meteor.call() not recognized?

时间秒杀一切 提交于 2019-12-25 03:00:34

问题


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?


回答1:


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).




回答2:


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.



来源:https://stackoverflow.com/questions/33291926/why-is-meteor-call-not-recognized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!