Diberikan contoh kode dari 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));

Bagaimana Anda mendapatkan kode kesalahan dan pesan kesalahan?

Inilah hasilnya (sesuai panduan cepat)

{
  "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"
}

Saya mencoba ini:

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));

Tapi tidak bekerja. Saya mendapatkan token yang tidak terduga. Idealnya saya ingin mencatat kode kesalahan dan pesan dan informasi lain seperti date_sent, dll. jadi saya memvalidasi dan mengirim info yang benar kembali ke klien.

Bantuan apa pun akan sangat dihargai.

Terima kasih

2
GasparYYC 18 Maret 2019, 21:29

1 menjawab

Jawaban Terbaik

Anda mendapatkan hal token karena Anda memiliki titik koma di antara keduanya then() (perhatikan akhir baris pertama dan awal baris kedua dalam kode di bawah)

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

Jadi, coba sesuatu seperti ini:


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

2
Alex Baban 18 Maret 2019, 19:21