首页 > 解决方案 > 来自 Api 的数据顺序不正确?

问题描述

在我的应用程序中,我使用 REST API 从数据库中获取数据。当我在 Postman 中检查 API 请求的响应时,它显示一种格式,当我得到响应并记录数据时,它显示另一种与我要求的完全不同的顺序。

可能是什么问题?请指导我。

邮递员回复:

{
    "testdata": {
        "attributes": {
            "year": {
                "2019": [
                    {
                        "id": 1,
                        "y": 2019,
                        "q": 1,
                    }
                ],
                "2018": [
                    {
                        "id": 2,
                        "y": 2018,
                        "q": 1,
                    },
                    {
                        "id": 3,
                        "y": 2018,
                        "q": 4,
                    }
                ]
            },
        }
    }
}

我的组件.ts

this.testService.getdata().subscribe(d => {
    console.log(d.testdata.attributes.year);
});

我得到这个结果

2018 Array(2)
2019 Array(1)

代替

2019 Array(1)
2018 Array(2)

测试服务.ts

getdata() {
    return this
      .http
      .get(`${environment.apiUrl}` + '/test/data');
}

标签: angulartypescriptapihttp

解决方案


推荐阅读