javascript - API 调用不会返回数组中的所有对象

标签 javascript json react-native

我正在调用这个API https://reqres.in/api/users?page=1我无法在数据属性中找到所有三个对象。我只能看到第一个对象。

这是我正在使用的代码

        categoryApi.getOne(1).then(response => {
            if (response.data) {
                console.log('categories: ', response.data.data);
            }
        });

这是响应:

Object {
 "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg",
   "first_name": "George",
   "id": 1,
   "last_name": "Bluth",
 }

axios 调用函数如下:

getOne: id => {
        return axios.get('users?page=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;
            }
        });
    },

知道为什么它只获取第一个对象而不获取所有三个对象吗?

最佳答案

因为你还在

中传递了id
params: {id: id}.

不要传递params,或者传递空的params,仅此而已。

关于javascript - API 调用不会返回数组中的所有对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49908924/

相关文章:

javascript - 我应该如何放置一个函数来 react native 开关?

react-native - 将 React-Navigation 中的状态从 TabsNavigation 传递到子 StackNavigation

javascript - 文本框值未传递给 api 调用

javascript - 软件故障注入(inject)的真正用途是什么?

javascript - jQuery - 在 HTML PRE 中过滤 JSON 数组

json - 使用动态字段名称在 Angular 中构造 json 对象

reactjs - redux-saga中间件如何处理api错误

javascript - 字体大小增加和对比度/亮度,使用 jquery 的网站声音控制

javascript - 是否可以通过 class 而不是 id 跳转到 HTML 元素?

ruby - 通过忽略 cucumber 中的顺序断言两个 json 内容