如何解决使用 axios 从 twilio 运行时获取实时 json 数据
我正在尝试从 twilio 无服务器功能中获取实时数据。我正在使用稍微编辑的样板函数。我想要的是服务器中的 json 数据和呼叫中的语音响应连续。但以下代码没有将 json 数据发送到服务器。
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);
};
它显示以下错误,但如果我不使用语音响应回调 return callback(null,twiml) 而是使用简单的 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...
解决方法
return callback(null,twiml);
应该在 .then
块中。
.then((response) => {
console.log(JSON.stringify(response.data));
return callback(null,twiml);
})
此外,该错误表明已达到 3000 毫秒超时,您的应用程序是否返回 200-OK?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。