简体   繁体   English

为什么使用 Twilio 获得意外令牌?

[英]Why getting unexpected token with Twilio?

Given the sample code from Twilio:鉴于 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.理想情况下,我想记录错误代码和消息以及其他信息,如 date_sent 等。所以我验证并将正确的信息发送回客户端。

Any help will be greatly appreciated.任何帮助将不胜感激。

thanks谢谢

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)你得到token是因为你在两个then()之间有一个分号(观察下面代码中第一行的结尾和第二行的开头)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM