Kav*_*. N 11 mysql fetch node.js reactjs react-native
下面的代码总是返回下面的有线对象
{“_U”:0,“_V”:0,“_W”:空,“_X”:空}
作为回应。
这是我的代码
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();
}
Run Code Online (Sandbox Code Playgroud)
我使用 node、express、Mysql 作为后端和 react-native 前端
我的后端代码在这里
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);
});
});
Run Code Online (Sandbox Code Playgroud)
上面的代码在控制台中给出了正确的输出,但 fetch API 不是。
我找不到我的问题的解决方案。提前致谢。
lim*_*mco 18
这表明您在解决之前记录了承诺 - 当您:
console.log(response.json())
正如@Evert 在评论中正确指出的那样,这是因为 response.json() 返回一个 promise 对象。
因此,.then()在调用response.json()记录已解决承诺的位置后,您需要链接一个额外的。
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));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8911 次 |
| 最近记录: |