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

{"_: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相关问答推荐

'无法解析模块.安装react-native-pager-view后的实用程序/平台

如何将S的此API输出输出到平面列表中?控制台将数据及其可视记录到平面列表中,但我无法输出到平面列表中

ReactNative如何在填充编辑表单 timeshift 除按钮禁用状态

React Native:任务 ':app:checkDebugDuplicateClasses' 执行失败

如何在 React Navigation 5 中将父函数传递给子屏幕?

变量 window在react-native 中代表什么?

在 React Native 中计算两个 lat+lng 坐标之间的距离?

React-Native Android 中隐藏(hide)的元素溢出

将 svg 转换为 react-native-svg

为什么,确切地说,我们需要 React.forwardRef?

如何在不使用 JSX 的情况下在 React Native 中制作组件?

如何使用特定目标运行 react-native run-ios

React Native 得到这个错误'Unrecognized operator abs'

如何将props传递给 React Navigation 导航器中的组件?

如何知道 FlatList 中的滚动状态?

在 FlatList 上使用 React Native 搜索过滤器

React Native Android 负边距(Margins)

如何使 react-native Picker 停留在新 Select 的选项?

使用 React Navigation 导航堆栈时重新渲染组件

发布未使用 JavaScript 代码更新的 APK