Wednesday, 12 April 2017

Unable to get values from JSON in NodeJS

I am trying to check on values returned from a query. This result is in JSON format. My code work, until my condition statement. While debugging I found that my condition statement was always failing because the value I am trying to retrieve from JSON and compare is always undefined.

    var promise = insertDataInMongo([body, token]);

            var newBody = JSON.parse(body);

            if (newBody.paging.facebookPost !== -1 || newBody.paging.youtube !== -1 || newBody.paging.twitter !== -1 || newBody.paging.facebook !== -1 || newBody.paging.reddit !== -1 || newBody.paging.youtubeComment !== -1 || newBody.paging.news !== -1)
                promise.then(getAthleteMedia([athlete, token, newBody.paging.facebook, newBody.paging.facebookPost, newBody.paging.reddit, newBody.paging.youtube, newBody.paging.youtubeComment, newBody.paging.twitter, newBody.paging.news]))

The body that gets returned looks like this:

     { "paging": [
{
  "youtubeComment": -1
},
{
  "youtube": -1
},
{
  "twitter": -1
},
{
  "facebook": -1
},
{
  "reddit": -1
},
{
  "facebookPost": -1
},
{
  "news": -1
}

]}

When I debug, I see the following:

JSON Values

However, the parse values are showing as:

Parsed values

Any help is much appreciated. Thanks,



via SAS

No comments:

Post a Comment