问题
Quick question (I hope) - I'm trying to use the Meteor Twilio package ("mrt add twilio") to send SMSs from my app.
Everything is set up correctly (and I pre-installed Moment as mentioned in the docs), I think... however I'm getting a "Twilio is not defined" error when my code is run in the event handler.
Presumably this is something to do with the way the package 'requires' the NPM package code? Has anyone else faced this?
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)
}
});
}
});
回答1:
The code you have used runs only on the server side. You would need to interface it with a method/call
Server side:
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)
}
});
}
});
Then on the client..
Meteor.call("sendsms", "something", "something");
You may need to consider using synchronous code or wrap your twilio.sendSms in Meteor._wrapAsync to get a result back on the client.
来源:https://stackoverflow.com/questions/23714958/meteor-twilio-mrt-package