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

时间:2014-11-30 08:05:39

标签: javascript meteor npm twilio

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

我正在使用找到here的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."
            }
        });
    }
});

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

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)

除了将mrt:moment和mrt:twilio-meteor包添加到项目中之外,我没有再进行任何设置。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:5)

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