mysql - fetch API 总是返回 {"_U": 0, "_V": 0, "_W": null, "_X": null}

标签 mysql node.js reactjs react-native fetch

下面的代码总是返回下面的有线对象

{"_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 前端
我的后端代码在这里
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 不是。
我找不到我的问题的解决方案。提前致谢。

最佳答案

这表明您在解决之前记录了 promise - 当您:console.log(response.json()) How do I access promise callback value outside of the function?
正如@Evert 在评论中正确指出的那样,这是因为 response.json() 返回一个 promise 对象。
因此,您需要在调用 .then() 后链接一个额外的 response.json() 来记录已解决的 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));
} 

关于mysql - fetch API 总是返回 {"_U": 0, "_V": 0, "_W": null, "_X": null},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64906396/

相关文章:

MySQL - 使用 JOIN 从表中选择最低值

node.js - AdonisJS 5、动态连接数据库

reactjs - 如何正确拆分调度和状态,以免重新渲染?

mysql - Nodejs.Mysql 模块未安装

MYSQL:多表连接 - 以先前的连接为条件

node.js - 如何使用 Express 将 powershell 命令结果推送到 NodeJs 中的 json 响应?

node.js - 客户端(angular 2)身份验证, Passport 谷歌作为服务器(node.js)中的提供者

javascript - 提交后 React.js "hide element and reveal next element"

reactjs - browserHistory.push() 和 context.router.push() 有什么区别

mysql - 无法通过 Ubuntu 服务器连接到远程 MySQL 数据库