Trying to use Twilio with Meteor, ReferenceError: Twilio is undefined

As a preface to this, I'm very new to Meteor and have never used Twilio before, so I'm probably just making a stupid mistake somewhere.

I am using the Twilio API bindings found here and trying to get a simple piece of code that works to send an SMS message inside Meteor .methods. Here's the event trigger and method function:

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."
            }
        });
    }
});

      

So when this event fires, I get the following error:

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)

      

Apart from adding the mrt: moment and mrt: twilio-meteor packages to the project, I did not configure any more. Any help is appreciated.

+3


source to share


1 answer


You have defined your method on both the client and the server. But the symbol Twilio

doesn't even show up on the client (because that's something the client doesn't need to know about). Hence, you are getting this error. Put your method definition sendSMS

in a block Meteor.isServer

and it should work fine.



+5


source







All Articles