fetch API 总是返回 {“_U":0,“_V":0,“_W":null,“_X&q

2023-09-17数据库问题
50

本文介绍了fetch API 总是返回 {“_U":0,“_V":0,“_W":null,“_X":null}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

The below code always return the below wired object

{_U":0,_V":0,_W":空,_X":空}

{"_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())

如何访问 promise 回调值在函数之外?

正如@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));
} 

这篇关于fetch API 总是返回 {“_U":0,“_V":0,“_W":null,“_X":null}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Mysql目录里的ibtmp1文件过大造成磁盘占满的解决办法
ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致...
2025-01-02 数据库问题
151

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

MySQL groupwise MAX() 返回意外结果
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)...
2024-04-16 数据库问题
13

MySQL SELECT 按组最频繁
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)...
2024-04-16 数据库问题
16

为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同
Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)...
2024-04-16 数据库问题
13

MySQL GROUP BY DateTime +/- 3 秒
MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)...
2024-04-16 数据库问题
14