给出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));
如何获取错误代码和错误消息?
这是输出(根据快速指南)
{
"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"
}
我正在尝试:
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));
但是不起作用。我得到了意外的令牌。 理想情况下,我想记录错误代码和消息以及其他信息,例如date_sent等。因此,我进行验证并将正确的信息发送回客户端。
任何帮助将不胜感激。
谢谢
答案 0 :(得分:2)
之所以得到token
是因为两个then()
之间有分号(请在下面的代码中观察第一行的结尾和第二行的开头)
.then(message => console.log(message.sid)); // <----
.then(message => console.log(message.error_code));
所以,尝试这样的事情:
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');
});