meteorTwilio MRT包

快速的问题(我希望) – 我正在尝试使用Meteor Twilio软件包(“mrt add twilio”)从我的应用程序发送短信。

一切都设置正确(我预先安装Moment正如在文档中提到的),我认为…但是,当我的代码在事件处理程序中运行时,我得到“Twilio未定义”错误。

据推测,这是与包“需要”的NPM包代码的方式? 有没有其他人面对这个?

Template.registration.events({ 'click #registerButton': function (e) { e.preventDefault(); var accountSid = 'AC758eaf30370ee2ba8b64f64ce19769c8'; var authToken = 'f8a0ee4560d3368a461e1f751b98fd90'; twilio = Twilio(accountSid, authToken); //this appears to be the issue twilio.sendSms({ to:'+966533444837', from: '+18654072438', body: 'Hi this is a test from Twilio.' }, function(err, responseData) { if (!err) { console.log(err) } }); } }); 

您使用的代码只在服务器端运行。 你需要用一个方法/调用来连接它

服务器端:

 Meteor.methods({ sendsms:function(param1, param2..) { var accountSid = 'AC758eaf30370ee2ba8b64f64ce19769c8'; var authToken = 'f8a0ee4560d3368a461e1f751b98fd90'; twilio = Twilio(accountSid, authToken); //this appears to be the issue twilio.sendSms({ to:'+966533444837', from: '+18654072438', body: 'Hi this is a test from Twilio.' }, function(err, responseData) { if (!err) { console.log(err) } }); } }); 

然后在客户端

 Meteor.call("sendsms", "something", "something"); 

您可能需要考虑使用同步代码或将您的twilio.sendSms包装到Meteor._wrapAsync以在客户端上获取结果。