为什么Meteor.call()无法识别?

时间:2015-10-22 22:37:42

标签: javascript rest meteor http-get

这个问题从其ancestor变得足够明显,建议我发布一个新问题。

当我打电话给我的" insertPerson "方法,然后调用我的" getTextAddrAsEmailAddr "方法,我看,在控制台中,我的两个调试console.log msgs:

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

......接着是这个例外:

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)

注意:methods.js中的第37行是:

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

对于上下文,整个methods.js文件是:

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

我的Meteor.http.call()调用有问题吗?

2 个答案:

答案 0 :(得分:2)

根据docs,它应该是HTTP.call()而不是Meteor.http.call()

(另请确保您已使用meteor add http添加了包。)

答案 1 :(得分:1)

好吧,它不是在文档中写成的事实,但我很确定,Meteor.call()应该从客户端到服务器进行远程调用。

此外,我想知道,为什么当你已经“在服务器端”时,你会在这一点上制作一个Meteor.call()

尝试:

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

如果你想让函数getTextAddrAsEmailAddr也可用作Meteor方法,只需添加:

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

希望能为你解决这个问题

干杯 汤姆

更新

我想知道使用Meteor.call()

的开销和正确性
  1. 您可以随时随地使用

  2. 注意开销

  3. Meteor.call定义于:

    Package ddp-client/livedata_connection.js L665

    并将始终通过Meteor.apply运行,该时间定义于:

    Package ddp-client/livedata_connection.js L706

    如果您从L707-L912检查来源,我想它并不在意,直接调用该函数的建议效率更高。