get real-time json data from twilio runtime with axios

[亡魂溺海] 提交于 2021-01-07 02:49:37

问题


I am trying to achieve real-time data from twilio server-less function. I am using a boilerplate function edited a little bit.What I want is json data in server and voice response in call consecutively .but the following code is not sending json data to server.

const axios = require('axios');

exports.handler = function (context, event, callback) {
let twiml = new Twilio.twiml.VoiceResponse();
twiml.say('you are welcome ');


 const instance = axios.create({
  baseURL: 'http://fafc4eac4162.ngrok.io/',
  timeout: 3000,

  });

 instance
   .post('/test', {
    id: 1,
    title: 'Twilio'
  })
  .then((response) => {
    console.log(JSON.stringify(response.data));
 
  })
   .catch((error) => {
    console.log(error);
    return callback(error);
   });

  return callback(null, twiml);

  };

It shows below error,but it sends data successfully if I do not use the voice response callback return callback(null, twiml) and rather use simple return callback(null, response.data);

  {"message":"timeout of 3000ms exceeded","name":"Error","stack":"Error: timeout of 3000ms 
 exceeded\n    at createError (/var/task/node_modules/axios/lib/core/createError.js:16:15)\n    
 at RedirectableRequest.handleRequestTimeout 
 (/var/task/node_modules/axios/lib/adapters/http.js:280:16)\n    at Object.onceWrapper 
 (events.js:286:20)\n    at RedirectableRequest.emit (events.js:198:13)\n    at 
  Timeout._onTimeout (/var/task/node_modules/follow-redirects/index.js:166:13)\n    at 
  ontimeout (timers.j...

回答1:


The return callback(null, twiml); should be in the .then block.

  .then((response) => {
    console.log(JSON.stringify(response.data));
    return callback(null, twiml);
  })

Also, the error indicates the 3000ms timeout is hit, is your application returning a 200-OK?



来源:https://stackoverflow.com/questions/65548123/get-real-time-json-data-from-twilio-runtime-with-axios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!