[英]Could anyone tell me why my setTimeout function within the try block does not work?
I have a problem.我有个问题。 Could anyone tell me why my setTimeout function within the try block does not work?
谁能告诉我为什么 try 块中的 setTimeout function 不起作用? It doesn't wait for 10000 milliseconds and simple runs through.
它不会等待 10000 毫秒并且简单地运行。 As a result, the console shows the error message "cannot read property data of "undefined". The API should return an object, but needs some time to fetch the answer. The console.log(responseInformation) returns also "undefined".
结果,控制台显示错误消息“无法读取“未定义”的属性数据。API 应该返回 object,但需要一些时间来获取答案。console.log(responseInformation) 也返回“未定义”。
const fetchAnswerFromDialogflow =
try {
const res = await axios.post(
`https://dialogflow.googleapis.com/v2/projects/myteachingbot-arxmxd/agent/intents:batchUpdate`,
node,
config
);
const waitForResponseAssignment = async () => {
let nodesSavedToChatbot = await res.response.data;
return nodesSavedToChatbot;
};
const responseInformation = setTimeout(
await waitForResponseAssignment(),
10000
);
console.log(responseInformation);
The problems in your code:您的代码中的问题:
const res = await axios.post(
`https://dialogflow.googleapis.com/v2/projects/myteachingbot-arxmxd/agent/intents:batchUpdate`,
node,
config
);
const waitForResponseAssignment = async () => {
/*
axios response doesnt have a property called response,
so res.response.data is error.
Also res.data is already a response from the api(res is
not a promise so you dont have to use await below,
even you dont need this `waitForResponseAssignment` function)
*/
let nodesSavedToChatbot = await res.response.data;
return nodesSavedToChatbot;
};
// This timeout function is not correct, setTimeout
// accepts the first param as callback(but you passed a value)
// Actually you also dont need this `setTimeout`
const responseInformation = setTimeout(
await waitForResponseAssignment(),
10000
);
You can just use the below code:您可以使用以下代码:
const res = await axios.post(
`https://dialogflow.googleapis.com/v2/projects/myteachingbot-arxmxd/agent/intents:batchUpdate`,
node,
config
);
return res.data;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.