【问题标题】:react native api call response反应原生 api 调用响应
【发布时间】:2018-04-18 14:43:43
【问题描述】:

我正在发出一个 http 调用请求,但由于某种原因,响应有点奇怪!

我的电话看起来像:

      getOne: id => {
            return axios.get('posts/1', {
                params: {
                    id: id
                }
            }).then(function (response) {
                return response;
            }).catch(function (error) {
                if (error.response) {
                    // The request was made and the server responded with a status code
                    return error;
                } else if (error.request) {
                    // The request was made but no response was received
                    return error.request;
                } else {
                    // Something happened in setting up the request that triggered an Error
                    return error.message;
                }
            });
        },

从组件中我称之为

categoryApi.getOne(1).then(response => {
            if (response.data) {
                this.setState({isLoading: false});
                console.log(response.data);
                this.setState({categories: this.state.categories.push(response.data)});
                console.log(this.state.categories);
            } else {
                this.setState({isLoading: false});
                Alert.alert(
                    'Something wrong happened!',
                    'My Alert Msg',
                    [],
                    {cancelable: true}
                )
            }
        });

在控制台中,响应如下所示:

     Object {
      "body": "quia et suscipit
       suscipit recusandae consequuntur expedita et cum
       reprehenderit molestiae ut ut quas totam
       nostrum rerum est autem sunt rem eveniet architecto",
       "id": 1,
       "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
       "userId": 1,
     }

问题是我希望响应是像这样的对象:

{
          "body": "quia et suscipit
           suscipit recusandae consequuntur expedita et cum
           reprehenderit molestiae ut ut quas totam
           nostrum rerum est autem sunt rem eveniet architecto",
           "id": 1,
           "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
           "userId": 1,
         }

我需要更改什么以使我没有 Object{} 而只有 {}

【问题讨论】:

    标签: javascript json react-native


    【解决方案1】:

    尝试在您的 JS 控制台中输入以下内容:

    var myObject = {a: 1, b: 2}

    然后检查你的新对象:

    myObject

    它应该显示为:

    Object { a: 1, b: 2 }

    {} 和 Object{} 是同一个东西。后者是控制台选择显示 JavaScript 对象的方式,因为它们具有与之关联的其他数据,如果您单击以展开对象,您可以看到这些数据:

    {...} a: 1 b: 2 __proto__: Object {...}

    查看本教程以获取working with objects

    【讨论】:

    • 我没有得到你的答案,那么为什么在使用 push 向数组添加对象时我不能使用 .map。
    • this.state.categories.map((item, key)=>( 不工作
    • js 对象上没有原生映射。你可以这样做Object.entries(myObject).map( ([key, value]) => console.log('the key is', key); console.log('the value is', value); )
    • 我实际上有一个对象数组,如何循环遍历每个对象?您提供的代码似乎不适合我
    • 我也只将第一个对象添加到数组中,我知道为什么在 data 属性中有例如三个不同的对象:reqres.in/api/users?page=1
    猜你喜欢
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 2018-09-01
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    相关资源
    最近更新 更多