快速提问(我希望如此)- 我正在尝试使用 Meteor Twilio 程序包(“mrt add twilio”)从我的应用程序发送 SMS。
一切都已正确设置(我预装了 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
中以在客户端上返回结果。
关于javascript - Meteor Twilio MRT 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23714958/