首先声明,我对于Meteor和Twilio都很陌生,所以可能会有些愚蠢的错误。
我正在使用在这里找到的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 packages添加到项目中,我没有进行任何其他设置。非常感谢您的帮助。