Rhys Edwards
Rhys Edwards

Reputation: 791

TypeError: Cannot read property 'sid' of undefined

I'm trying to send off an sms (which works in Twilio's API Explorer) but seems to fail under my node installation. I've just done a complete uninstall and reinstalled with no avail.

Error

7 Oct 21:28:37 - [nodemon] starting `node scraper.js`
Free on Xbox One, Xbox 360, PS3, PS4: Tales from the Borderlands (Episode 1)
/Users/rhysedwards/Downloads/insightful/ozbargain/node_modules/twilio/node_modules/q/q.js:126
                    throw e;
                    ^

TypeError: Cannot read property 'sid' of undefined
    at /Users/rhysedwards/Downloads/insightful/ozbargain/scraper.js:39:31
    at /Users/rhysedwards/Downloads/insightful/ozbargain/node_modules/twilio/node_modules/q/q.js:1924:17
    at flush (/Users/rhysedwards/Downloads/insightful/ozbargain/node_modules/twilio/node_modules/q/q.js:108:17)
    at doNTCallback0 (node.js:408:9)
    at process._tickCallback (node.js:337:13)
7 Oct 21:28:39 - [nodemon] app crashed - waiting for file changes before starting...

Error with stripped back twilio code;

7 Oct 22:24:44 - [nodemon] starting `node scraper.js`
/Users/rhysedwards/Downloads/insightful/ozbargain/node_modules/twilio/node_modules/q/q.js:126
                    throw e;
                    ^

TypeError: Cannot read property 'sid' of undefined
    at /Users/rhysedwards/Downloads/insightful/ozbargain/scraper.js:12:24
    at /Users/rhysedwards/Downloads/insightful/ozbargain/node_modules/twilio/node_modules/q/q.js:1924:17
    at flush (/Users/rhysedwards/Downloads/insightful/ozbargain/node_modules/twilio/node_modules/q/q.js:108:17)
    at doNTCallback0 (node.js:408:9)
    at process._tickCallback (node.js:337:13)
7 Oct 22:24:46 - [nodemon] app crashed - waiting for file changes before starting...

Code

var accountSid = 'AC*******';
var authToken = 'da********';

var fs = require('fs'),
    request = require('request'),
    cheerio = require('cheerio'),
    client = require('twilio')(accountSid, authToken);

url = 'http://www.ozbargain.com.au';

request(url, function(error, response, html) {
    if (!error && response.statusCode == 200) {
        var $ = cheerio.load(html);
        var $el = $("a:contains('Xbox')");

        if ($el.length) {
          client.messages.create({
            to: "61448141065",
            from: "+61418739508",
            body: "hey",
          }, function(err, message) {
            console.log(message.sid);
          });
            console.log($el.text());
        } else {
            console.log('hey');
        }
    }
});

Upvotes: 3

Views: 4902

Answers (4)

wisbucky
wisbucky

Reputation: 37797

It's because your call failed and there is no message object. Replace

console.log(message.sid);

with

if (err) console.log(err.message);
if (message) console.log(message.sid);

Then you will see the error message.

Upvotes: 0

Bordin
Bordin

Reputation: 21

You may simply change console.log(message.sid); to

if(err) {
    console.log(err.message);
}

Upvotes: 2

Marcos Placona
Marcos Placona

Reputation: 21720

Twilio developer evangelist here.

There are a couple of things wrong on your code on the twilio side of things.

client.messages.create({
    to: "61448141065",
    from: "+61418739508",
    body: "hey",
}, function (err, message) {
    console.log(message.sid);
});

on the Toyou're missing a + before the phone number, and after the body, you have an extra comma which you need to remove.

Your final code should look like the following:

var accountSid = 'AC*******';
var authToken = 'da********';

var fs = require('fs'),
    request = require('request'),
    cheerio = require('cheerio'),
    client = require('twilio')(accountSid, authToken);

url = 'http://www.ozbargain.com.au';

request(url, function(error, response, html) {
    if (!error && response.statusCode == 200) {
        var $ = cheerio.load(html);
        var $el = $("a:contains('Xbox')");

        if ($el.length) {
          client.messages.create({
            to: "+61448141065",
            from: "+61418739508",
            body: "hey"
          }, function(err, message) {
            console.log(message.sid);
          });
            console.log($el.text());
        } else {
            console.log('hey');
        }
    }
});

I tested it here and it worked fine after changing that.

Hope this helps you.

UPDATE: Try changing your code to just this:

var accountSid = 'AC*******';
var authToken = 'da********';

var fs = require('fs'),
    client = require('twilio')(accountSid, authToken);

client.messages.create({
    to: "+61448141065",
    from: "+61418739508",
    body: "hey"
}, function (err, message) {
    console.log(message.sid);
});

Upvotes: 0

Tushar
Tushar

Reputation: 87203

The callback of client.messages.create is used as

}, function(err, message) {
    console.log(message.sid);
});

When there will be error, the first parameter of the callback err will contain the information related to the error, and the second parameter message will be undefined.

Update the code as follow to handle the erroneous conditions

}, function (err, message) {
    if (err) {
        // Handle error
        // Show appropriate message to user
    } else {
        // No error

        if (message.sid) {
            // Use sid here

        }
    }
    console.log(message.sid);
});

Upvotes: 2

Related Questions