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

{"_: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 解决之前就记录了它——当你:

How do I access promise callback value outside of the function?

正如@Evert在 comments 中正确指出的,这是因为回应.json()返回一个promise对象.

因此,在您拨打response.json()并记录已解决的promise 后,您需要再连接.then()个.

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));
} 

React-native相关问答推荐

我们如何才能将我们自己的应用程序(IOS)排除在共享表中?

应用管理

在 React Native 上为 android 创建自定义 UI 组件,如何向 JS 发送数据?

react-native (expo)加载markdown 文件

React Native - 如何判断 UI/元素?

React-Native + crypto:如何在 React-Native 中生成 HMAC?

@react-navigation/stack 与 @react-navigation/native-stack 有什么区别?

React Native localhost 另一个调试器已经连接

React Native:fetch request failed with error - TypeError: Network request failed(…)

如何在 react-native 上自动聚焦下一个 TextInput

试图注册两个具有相同名称 ProgressBarAndroid 的视图

Fragment片段内的react-native

如何使用 Expo 在真实的 iOS 设备上运行应用程序?

React Native onLayout 与 React Hooks

React Native Bullet Character? or Unicode?

更改导航标题背景 colored颜色

如何设置样式属性以填充剩余空间?

如何在 react-native 中设置 Alert 元素的样式?

为什么 this.state 在react-native中未定义?

React Native 构建命令失败:PhaseScriptExecution ... (domain=NSPOSIXErrorDomain, code=2)