【发布时间】:2025-11-22 13:50:01
【问题描述】:
在这里,我实现了一个名为 fetchDetails 的函数,我用它来使用回调获取数据。 根据我的说法,调用函数/它的回调的语法不正确。 它每次都会触发“回调不是函数”的错误。 如何调用回调以从 fetchDetails 获取该数据。
已经尝试返回数据而不是回调(数据);
var flag = false;
function fetchDetail(serise, callback) {
if(responseData.body.data.length - 1 !== serise){
// process to fetch data from array of responsedata
}
else
{
// Here if the serise is reached at length of the array then will true flag
flag = true;
}
if(flag){
//if the flag is true then, wanted to get data which is callback by function fetchDetails()
callback(); // Here my callback get stuck and get error "Callback is not a function"
return;
}
else
{
// Here increase serise for getting next element of an array
fetchMessage(++serise);
}
}
fetchDetail(0,function(){
let data ={
isFound: 1,
detail: "detail found"
}
callback(data);
});
预期结果是获取fetchDetail()中定义的数据,但进程卡在
if (flag) {
callback();
}
【问题讨论】:
-
为什么不
flag = serise === (responseData.body.data.length - 1); if (!flag) { /* process to fetch data from array of responsedata */ } -
您在
callback(data)中收到错误消息。因为那里callback没有任何意义。 -
@mplungjan 我需要关于回调的解决方案。
-
@deceze 我是 nodejs 和 javascript 的新手。我如何得到回应。
标签: javascript node.js asynchronous callback