提取API始终返回{"_U":0,"_ V":0,"_ W":null,"_ X":null} [英] fetch API always returns {"_U": 0, "_V": 0, "_W": null, "_X": null}
问题描述
下面的代码总是返回下面的有线对象
The below code always return the below wired object
{"_ U":0,"_ V":0,"_ W":null,"_ X":null}
{"_U": 0, "_V": 0, "_W": null, "_X": null}
作为回应.
这是我的代码
getData = () => {
fetch('http://192.168.64.1:3000/getAll',{
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
})
.then((response) => {
console.log('Response:')
console.log(response.json())
console.info('=================================')
})
.catch(err => console.error(err));
}
componentDidMount(){
this.getData();
}
我正在使用node,express,Mysql作为后端和react-native前端
I am using node, express, Mysql as backend and react-native frontend
我的后端代码在这里
app.get('/getAll',(req,res) => {
console.log('getAll method Called');
con.query('select * from dummy',(err,results,fields) => {
if(err) throw err;
console.log('Response');
console.log(results);
res.send(results);
});
});
上面的代码在控制台中提供了正确的输出,但fetch API却没有.
The above code gives correct output in console but fetch API is not.
我找不到解决我问题的方法.预先感谢.
i cant find solution for the my problem. Thanks in advance.
推荐答案
这表示您在解决承诺之前记录了承诺-当您执行以下操作时的结果: console.log(response.json())
That indicates that you are logging the promise before it resolves - the result of when you:
console.log(response.json())
正如@Evert在注释中正确指出的,这是因为response.json()返回了一个Promise对象.
As @Evert rightfully pointed out in comments, this is because response.json() returns a promise object.
因此,您需要在调用 response.json()
并记录已解决的承诺后,再链接一个附加的 .then()
.
So, you'll need to chain an additional .then()
after you call response.json()
where you log the resolved promise.
getData = () => {
fetch('http://192.168.64.1:3000/getAll',{
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
})
.then(response => response.json())
.then(data => {
console.log(data);
})
.catch(err => console.error(err));
}
这篇关于提取API始终返回{"_U":0,"_ V":0,"_ W":null,"_ X":null}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!