在FETCH API中记录{&Q;_U&Q;:0,&Q;_V&Q;:0,&Q;_W&Q;:空,&Q;_X&Q;:空} [英] LOG {"_U": 0, "_V": 0, "_W": null, "_X": null} inside fetch API
本文介绍了在FETCH API中记录{&Q;_U&Q;:0,&Q;_V&Q;:0,&Q;_W&Q;:空,&Q;_X&Q;:空}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在控制从后端获取数据的函数返回的数据时收到此错误
{&Q;_U&Q;:0,&Q;_V&Q;:0,&Q;_W&Q;:NULL,&Q;_X&Q;:NULL}
以下是代码:
const events_data = [];
function getvals() {
return fetch('http://PCIP:3000/users/timetable')
.then((response) => response.json())
.then((output) => {
return addData(output, events_data);
})
.catch(error => console.log(error))
}
function addData(data, data2) {
data.map((d) => {
data2.push({
title: d.name,
startTime: genTimeBlock(d.day, d.start_time),
endTime: genTimeBlock(d.day, d.end_time),
location: d.location,
extra_descriptions: [d.extra_descriptions],
});
});
}
const data = getvals();
console.log(data); // the error come from here
我已经检查了这里的答案,但没有一条对我起作用
fetch API always returns {"_U": 0, "_V": 0, "_W": null, "_X": null}
推荐答案
这是因为Fetch Promise尚未返回响应
有两种方法可以解决此问题,首先创建另一个异步函数并使用它等待响应
const events_data = [];
async function getvals() {
return fetch('http://PCIP:3000/users/timetable')
.then((response) => response.json())
.then((output) => {
return addData(output, events_data);
})
.catch(error => console.log(error))
}
function addData(data, data2) {
data.map((d) => {
data2.push({
title: d.name,
startTime: genTimeBlock(d.day, d.start_time),
endTime: genTimeBlock(d.day, d.end_time),
location: d.location,
extra_descriptions: [d.extra_descriptions],
});
});
}
async function waitForResponse() {
let resp = await getvals();
return resp;
}
const data = waitForResponse();
console.log(data); // the error come from here
另一种方法是使用状态挂钩,将返回的obj传递给响应上的状态挂钩。
这篇关于在FETCH API中记录{&Q;_U&Q;:0,&Q;_V&Q;:0,&Q;_W&Q;:空,&Q;_X&Q;:空}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文