简体   繁体   中英

angularJS not parsing array of strings

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM