Update: 02/12/2015 As mentioned in the comments this was an issue with the object being modified by an Angular module. I was able to track this down using toJSON
as mentioned below.
更新:02/12/2015正如评论中所提到的,这是Angular模块修改对象的问题。我能够使用toJSON跟踪这个,如下所述。
I have recently ran into an issue that has be quite boggled. I have a service that is request data from an api via get
request. When looking on the network panel in Chrome developer tools the api returns the proper results but they are not show in the response from the angular request.
我最近遇到了一个令人难以置信的问题。我有一个服务,是来自api的请求数据通过get请求。在Chrome开发人员工具中查看网络面板时,api会返回正确的结果,但它们不会显示在角度请求的响应中。
You can note the content section is missing items. And Angular code:
您可以注意到内容部分缺少项目。和角度代码:
Angular Code
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;
}
}
From 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" }
来自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”,更新时间:“2015-02-12T04:11:02.239Z”,内容:[“气球”,“公寓”,“测试”, “测试2”,“派对”,“Inez”],id:“54da44790eb10b0f00b453a1”}
Angular Scope / res.data in service { 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 Scope / 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”,更新时间:“2015-02-12T04:11:02.239Z”,内容:数组[3] 0:“气球”,1:“公寓“,2:”派对“]
1 个解决方案
#1
2
If the output from angular.toJson()
is correct, but the console.log
of the object is wrong, then it strongly suggests the object is being modified in the code somewhere after the call to console.log
.
如果angular.toJson()的输出是正确的,但对象的console.log是错误的,那么它强烈建议在调用console.log之后在代码中修改对象。
This is because console.log
takes a reference to an object, and some time later it turns it into a string for output. I would class it as slightly evil behaviour, as it suggests the bug is before the call to console.log
, while actually the issue after it. Hours can be wasted looking through the wrong code...
这是因为console.log接受对象的引用,一段时间后它将其转换为输出字符串。我会把它归类为有点邪恶的行为,因为它表明bug是在调用console.log之前,而实际上是它之后的问题。通过错误的代码可以浪费时间......
#1
2
If the output from angular.toJson()
is correct, but the console.log
of the object is wrong, then it strongly suggests the object is being modified in the code somewhere after the call to console.log
.
如果angular.toJson()的输出是正确的,但对象的console.log是错误的,那么它强烈建议在调用console.log之后在代码中修改对象。
This is because console.log
takes a reference to an object, and some time later it turns it into a string for output. I would class it as slightly evil behaviour, as it suggests the bug is before the call to console.log
, while actually the issue after it. Hours can be wasted looking through the wrong code...
这是因为console.log接受对象的引用,一段时间后它将其转换为输出字符串。我会把它归类为有点邪恶的行为,因为它表明bug是在调用console.log之前,而实际上是它之后的问题。通过错误的代码可以浪费时间......