尝试将Twilio与Meteor一起使用,ReferenceError:未定义Twilio

时间:2022-05-18 20:24:49

To preface this, I'm very new to Meteor and have never used Twilio before, so I'm probably just making a silly mistake somewhere.

为了序言,我对Meteor很新,之前从未使用过Twilio,所以我可能只是在某个地方犯了一个愚蠢的错误。

I'm using the Twilio API bindings found here and trying to get a simple snippet of code working to send an SMS message within a Meteor.methods function. Here's the event trigger and method function:

我正在使用这里找到的Twilio API绑定,并试图获得一个简单的代码片段,用于在Meteor.methods函数中发送SMS消息。这是事件触发器和方法功能:

if (Meteor.isClient) {
    Template.twilioPlayground.events({
        "click button": function() {
            Meteor.call("sendSMS");
        }
    });
}

Meteor.methods({
    sendSMS: function () {
        twilio = Twilio('i put my account sid here', 'and my auth token here');
        twilio.sendSms({
            to:'+7199634882', 
            from: '+17194530451', 
            body: 'This is a test'
        }, function(err, responseData) { //this function is executed when a response is received from Twilio
            if (!err) {
                console.log(responseData.from); // outputs "+14506667788"
                console.log(responseData.body); // outputs "word to your mother."
            }
        });
    }
});

So when that event is triggered, I'm getting the following error:

因此,当触发该事件时,我收到以下错误:

ReferenceError: Twilio is not defined
at Meteor.methods.sendSMS (http://localhost:3000/myTodoApp.js?8ae55884eab4c6a28ef9da8344fcf0b9d15c24ac:194:18)
at http://localhost:3000/packages/ddp.js?1f971b2ac9f4bdab7372cb5098ed1e26ff98dfb2:4239:25
at _.extend.withValue (http://localhost:3000/packages/meteor.js?61916b1060b33931a21f104fbffb67c2f3d493c5:945:17)
at _.extend.apply (http://localhost:3000/packages/ddp.js?1f971b2ac9f4bdab7372cb5098ed1e26ff98dfb2:4230:54)
at _.extend.call (http://localhost:3000/packages/ddp.js?1f971b2ac9f4bdab7372cb5098ed1e26ff98dfb2:4108:17)
at Object.Template.twilioPlayground.events.click button (http://localhost:3000/myTodoApp.js?8ae55884eab4c6a28ef9da8344fcf0b9d15c24ac:106:20)
at null.<anonymous> (http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:3103:18)
at http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:2371:30
at Object.Blaze._withCurrentView (http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:2029:12)
at null.<anonymous> (http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:2370:26)

Aside from adding the mrt:moment and mrt:twilio-meteor packages to the project, I didn't do any more setup. Any help is greatly appreciated.

除了将mrt:moment和mrt:twilio-meteor软件包添加到项目中之外,我没有再进行任何设置。任何帮助是极大的赞赏。

1 个解决方案

#1


5  

You defined your method on both client and server. But the Twilio symbol is not even exposed on the client (because it is something client doesn't need to know about). Hence you get this error. Put your sendSMS method definition into Meteor.isServer block and it should work fine.

您在客户端和服务器上定义了方法。但Twilio符号甚至没有暴露在客户端上(因为它是客户端不需要知道的东西)。因此,您收到此错误。将您的sendSMS方法定义放入Meteor.isServer块,它应该可以正常工作。

#1


5  

You defined your method on both client and server. But the Twilio symbol is not even exposed on the client (because it is something client doesn't need to know about). Hence you get this error. Put your sendSMS method definition into Meteor.isServer block and it should work fine.

您在客户端和服务器上定义了方法。但Twilio符号甚至没有暴露在客户端上(因为它是客户端不需要知道的东西)。因此,您收到此错误。将您的sendSMS方法定义放入Meteor.isServer块,它应该可以正常工作。