Angular 承诺从 JSON API 返回不正确的数据
Angular promise returning incorrect data from JSON API
更新:2015 年 2 月 12 日
正如评论中提到的,这是由 Angular 模块修改的对象的问题。如下所述,我能够使用 toJSON
追踪到这一点。
我最近 运行 遇到了一个一直很困惑的问题。我有一项服务是通过 get
请求从 api 请求数据。在 Chrome 开发人员工具的网络面板上查看 api returns 正确的结果,但它们未显示在 angular 请求的响应中。
您可以注意到内容部分缺少项目。和Angular代码:
Angular代码
function getPhotos(){
return $http.get('/photo')
.then(getPhotosComplete)
.catch(function(err){
//handle errors
});
function getPhotosComplete(res){
//Showing incorrect response here
console.log("getPhotosComplete", res);
return res.data;
}
}
来自API
{
url: "https://dev.amazonaws.com/060a2a5f-8bb7-4ffa-aa7d-e715439271a3.jpg",
archive_name: "140809",
seedcount: 0,
viewcount: 0,
live: true,
current: false,
next: false,
createdAt: "2015-02-10T17:48:41.505Z",
updatedAt: "2015-02-12T04:11:02.239Z",
content: [
"Balloon",
"Apartment",
"Testing",
"Testing 2",
"Party",
"Inez"
],
id: "54da44790eb10b0f00b453a1"
}
Angular 范围/res.data 投入使用
{
url: "https://dev.amazonaws.com/060a2a5f-8bb7-4ffa-aa7d-e715439271a3.jpg", seedcount: 0,
viewcount: 0,
live: true,
current: false,
next: false,
createdAt: "2015-02-10T17:48:41.505Z",
updatedAt: "2015-02-12T04:11:02.239Z",
content: Array[3]
0: "Balloon",
1: "Apartment",
2: "Party"
]
如果 angular.toJson()
的输出是正确的,但是对象的 console.log
是错误的,那么它强烈建议在调用 [=11 之后的某个地方在代码中修改对象=].
这是因为console.log
引用了一个对象,一段时间后它把它变成一个字符串输出。我会 class 它作为轻微的邪恶行为,因为它表明错误是在调用 console.log
之前,而实际上是在它之后的问题。查看错误的代码可能会浪费时间...
更新:2015 年 2 月 12 日
正如评论中提到的,这是由 Angular 模块修改的对象的问题。如下所述,我能够使用 toJSON
追踪到这一点。
我最近 运行 遇到了一个一直很困惑的问题。我有一项服务是通过 get
请求从 api 请求数据。在 Chrome 开发人员工具的网络面板上查看 api returns 正确的结果,但它们未显示在 angular 请求的响应中。
您可以注意到内容部分缺少项目。和Angular代码:
Angular代码
function getPhotos(){
return $http.get('/photo')
.then(getPhotosComplete)
.catch(function(err){
//handle errors
});
function getPhotosComplete(res){
//Showing incorrect response here
console.log("getPhotosComplete", res);
return res.data;
}
}
来自API
{
url: "https://dev.amazonaws.com/060a2a5f-8bb7-4ffa-aa7d-e715439271a3.jpg",
archive_name: "140809",
seedcount: 0,
viewcount: 0,
live: true,
current: false,
next: false,
createdAt: "2015-02-10T17:48:41.505Z",
updatedAt: "2015-02-12T04:11:02.239Z",
content: [
"Balloon",
"Apartment",
"Testing",
"Testing 2",
"Party",
"Inez"
],
id: "54da44790eb10b0f00b453a1"
}
Angular 范围/res.data 投入使用
{
url: "https://dev.amazonaws.com/060a2a5f-8bb7-4ffa-aa7d-e715439271a3.jpg", seedcount: 0,
viewcount: 0,
live: true,
current: false,
next: false,
createdAt: "2015-02-10T17:48:41.505Z",
updatedAt: "2015-02-12T04:11:02.239Z",
content: Array[3]
0: "Balloon",
1: "Apartment",
2: "Party"
]
如果 angular.toJson()
的输出是正确的,但是对象的 console.log
是错误的,那么它强烈建议在调用 [=11 之后的某个地方在代码中修改对象=].
这是因为console.log
引用了一个对象,一段时间后它把它变成一个字符串输出。我会 class 它作为轻微的邪恶行为,因为它表明错误是在调用 console.log
之前,而实际上是在它之后的问题。查看错误的代码可能会浪费时间...