Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Okay, I've been bashing my head bloody on this one:

I have the following JSON coming back from the server:

{
    "SendDate" : "2015-03-16T22:48:27.747",
    "SendTo" : {
        "ContactIds" : ["28a24538-cdfc-4453-920d-86f57d7eaf22"],
        "GroupIds" : []
    },
    "Message" : "MEETING TIME!!!!!"
}

I have checked this with several REST clients - this IS what comes back.

I have AngularJS "getting" this with an $http.get() operation, but I get an undefined on the "ContactIds" value - so, what I see in the JS Console is:

SendDate : "2015-03-16T22:48:27.747"
SendTo: 
  ContactIds: Array[1]
    0: undefined
    length: 1

I have NO IDEA what can be causing this.

Any ideas?

UPDATE: I have attached an interceptor and intercepted the response and the result is the same when I feed the data to the console - but when I use:

JSON.stringify(data)

I can see that the Data in the Array is THERE!

UPDATE 2:

Okay now this is driving me nuts. I have played with the interceptor and if I stringify the response and then use JSON.parse() - it works fine, but when I pass the response through, it comes out messed up again.

I traced it through angular's parsing process all the way to the "fromJson()" function. (code below:) It comes into the function as a string. (Now here's the Bizzarro part)

I altered the code like this:

function fromJson(json) {

    var obj1 = JSON.parse(json);
    console.log("Obj1:");
    console.log(obj1);

    //my altered angular code
    var obj2 = isString(json) ? JSON.parse(json) : json;  
    console.log("Obj2:");
    console.log(obj2);

    //  Pass to program...
    return obj1;
    //return obj2;

    /*  original angular code:
    return isString(json)
        ? JSON.parse(json)
        : json;
    */
}

If I run it and return obj1, the console logs obj1's ContactIds "0" index as "undefined" - but obj2 logs as "28a24538-cdfc-4453-920d-86f57d7eaf22".

"GREAT!", I'm thinking - so I return obj2, but now it logs undefined but obj1's "0" index is now the correct value. (WTH?)

So I reverse the code, just to see, and Return obj1 - and I'll be damned - obj2 returns "28a24538-cdfc-4453-920d-86f57d7eaf22" and obj1 is undefined. (It's like teasing a monkey.)

It HAS to be something later on in the pipeline that is doing it - OR - it may have something to do with the array being GUID strings - but I use GUID strings elsewhere with no problems.

It could also be another "angular process" that I'm unaware of that is causing this - angular is quite impressive.

Either way, I'm super-confused.

This is so stupid - I'm surprised that an array of strings is such a difficulty - and what's worse, it seems I'm the only one having this problem. (I researched this for six hours yesterday...)

Any other ideas, guys?

share|improve this question
    
Can you show how do you process the result of $http.get()? –  raina77ow Mar 10 at 14:09
    
Inspect the actual request in network tab of browser console to see what is actually received. SHow more code –  charlietfl Mar 10 at 14:41
1  
are you using angular.copy on your response object? i had an issue with this function when the object i wanted to clone had an uuid string in it ... don't ask me why, i just switched to JSON.parse/JSON.stringify (which is faster anyway) –  nilsK Mar 10 at 14:47
    
Yeah, the $http.get() is super simple - it's just $http.get(<api endpoint url>). As for the Network browser - I did show you the code.The JSON string is what I get back. As far as showing more code - I'm a minimalist on code - I have shown you what I have. No, I'm not using angular.copy() - I'm using everything out of the box from angularJS 1.4. Good thought though - thanks for sharing! :) –  user1628627 Mar 11 at 12:29

1 Answer 1

OH MY GOD I'M SO STUPID!!!

First I'd like to thank everyone for trying to help me.

The answer is this - there was no problem, that is, not with Angular or its parser.

The Problem is that the Console was logging transformed data, which had an error at MY controller. The data on MY end was not matching the data in the list that I had in my controller - (Database Error).

BOTTOM LINE:

If you have this error pop up, do NOT troubleshoot from the top-down - troubleshoot from the bottom-up.

Angular has many checks and balances - if you don't get "bleeding failures" throughout the program, chances are the error is in YOUR code.

Thank you everyone for your help.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.