GasparYYC
GasparYYC

Reputation: 188

Why getting unexpected token with Twilio?

Given the sample code from Twilio:

const accountSid = 'ACf37a37065e79fb1a7a594f294cb3b190';
const authToken = 'your_auth_token';
const client = require('twilio')(accountSid, authToken);

client.messages
  .create({
     body: 'This is the ship that made the Kessel Run in fourteen parsecs?',
     from: '+15017122661',
     to: '+15558675310'
   })
  .then(message => console.log(message.sid));

How do you get the error code and error message?

Here's the output (as per the quickguide)

{
  "account_sid": "ACf37a37065e79fb1a7a594f294cb3b190",
  "api_version": "2010-04-01",
  "body": "This is the ship that made the Kessel Run in fourteen parsecs?",
  "date_created": "Thu, 30 Jul 2015 20:12:31 +0000",
  "date_sent": "Thu, 30 Jul 2015 20:12:33 +0000",
  "date_updated": "Thu, 30 Jul 2015 20:12:33 +0000",
  "direction": "outbound-api",
  "error_code": null,
  "error_message": null,
  "from": "+15017122661",
  "messaging_service_sid": "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  "num_media": "0",
  "num_segments": "1",
  "price": -0.00750,
  "price_unit": "USD",
  "sid": "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  "status": "sent",
  "subresource_uris": {
    "media": "/2010-04-01/Accounts/ACf37a37065e79fb1a7a594f294cb3b190/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Media.json"
  },
  "to": "+15558675310",
  "uri": "/2010-04-01/Accounts/ACf37a37065e79fb1a7a594f294cb3b190/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json"
}

I'm trying this:

client.messages
  .create({
     body: 'This is the ship that made the Kessel Run in fourteen parsecs?',
     from: '+15017122661',
     to: '+15558675310'
   })
  .then(message => console.log(message.sid));
  .then(message => console.log(message.error_code));

But doesn't work. I get unexpected token. Ideally I want to log the error code and message and other information like date_sent, etc.. so I validate and send the right info back to the client.

Any help will be greatly appreciated.

thanks

Upvotes: 2

Views: 1854

Answers (1)

Alex Baban
Alex Baban

Reputation: 11732

You're getting the token thing because you have a semicolon between the two then() (observe the end of the first line and the beginning of the second line in code below)

  .then(message => console.log(message.sid)); // <----
  .then(message => console.log(message.error_code));

So, try something like this:


    client.messages
    .create({
       body: 'This is the ship that made the Kessel Run in fourteen parsecs?',
       from: '+15017122661',
       to: '+15558675310'
     })
     .then(message => {
        console.log(message.sid);
        console.log(message.error_code || 'no error');
    });

Upvotes: 2

Related Questions