1

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.

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.

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" }

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" ]

7
  • 1
    If you are using non-minified Angular, you can debug into Angular's code to see how Angular transform your response. AFAIK, Angular would try to transform your response to JSON object. Maybe something went wrong there. Commented Feb 12, 2015 at 4:35
  • 1
    Are you really sure? Try logging with JSON.stringify to make sure it's not just a display artifact. Array[3] 0: "Balloon", 1: "Apartment", 2: "Party" ] looks very suspicious.since it's not even JSON Commented Feb 12, 2015 at 4:39
  • @JuanMendes - I am sure, it is just the object that is returned in console. I have also use batarang and logged in many places. Commented Feb 12, 2015 at 4:41
  • Agree with @JuanMendes, Try the response logging using either by angular.toJson() or JSON.stringify(). Commented Feb 12, 2015 at 4:49
  • @JuanMendes - I have used your method. While the JSON is correct in the response the object is not updated with the proper values. Any ideas on cause of that? Commented Feb 12, 2015 at 5:09

1 Answer 1

3

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.

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...

Sign up to request clarification or add additional context in comments.

1 Comment

This fiddle may help explain what you mean with the console.log jsfiddle.net/mg1yu0fo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.