1

I am trying to check the values returned from a query. This result is in JSON format. My code works, 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,

John Moutafis
  • 22,254
  • 11
  • 68
  • 112
SAS
  • 13
  • 6

1 Answers1

1

What you are receiving is an array of objects not the objects of objects, therefore you should access them with indexes first then the property. Assuming the format of the JSON response is always same here is the working JSfiddle link and below is the sample code inspired from this How to get index of object by its property in javascript post's answer.

var newBody = {
  "paging": [{
    "youtubeComment": -1
  }, {
    "youtube": -1
  }, {
    "twitter": -1
  }, {
    "facebook": -1
  }, {
    "reddit": -1
  }, {
    "facebookPost": -1
  }, {
    "news": -1
  }]
};


function check(array, attr, value) {
  for (var i = 0; i < array.length; i += 1) {
    if (array[i][attr] === value) {
      return false;
    }
  }
  return true;
}

function get_value(array, attr) {
  for (var i = 0; i < array.length; i += 1) {
    if (array[i].hasOwnProperty(attr)) {
      return array[i][attr];
    }
  }
}

if (check(newBody.paging, "facebook", -1) || check(newBody.paging, "youtubeComment", -1) || check(newBody.paging, "youtube", -1) || check(newBody.paging, "twitter", -1) || check(newBody.paging, "reddit", -1) || check(newBody.paging, "facebookPost", -1) || check(newBody.paging, "news", -1)) {
  // promise.then(getAthleteMedia([athlete, token, get_value(newBody.paging, "facebook"), ... get_value(newBody.paging, "news")]));
  // similarly just pass the function in your promise, hope it clears how to get the values as well
  console.log(get_value(newBody.paging, "facebook"));

} else console.log("not in if");
Community
  • 1
  • 1
warl0ck
  • 3,356
  • 4
  • 27
  • 57
  • Sorry.. I am missing something, how is what you posted different than what I originally had? – SAS Apr 12 '17 at 08:00
  • sorry I missed to update the indexes, please recheck the jsfiddle link its updated to https://jsfiddle.net/44fakLh7/1/ – warl0ck Apr 12 '17 at 08:01
  • ok.. I suspected the same and tried this earlier. but what I find it, the sequence of objects in the returned array are not always the same. So I couldn't hardcode the indexes. – SAS Apr 12 '17 at 08:04
  • format of the JSON response is not always the same, hence the problem. – SAS Apr 12 '17 at 08:10
  • Thank you Warl0ck that would work! now my next step is to figure out, how to get the value form this array (non -1) and set that to the function I am calling inside the IF condition. 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])) – SAS Apr 12 '17 at 08:32
  • one thing you only want to enter the if condition if all of them are -1 ?? – warl0ck Apr 12 '17 at 08:39
  • no, I want to pass back the non -1 values to another function with corresponding attribute.. What is happening is I am getting a bunch of data form REST API. These in total are 50 elements. The 50 is spread across the 7 types of data. So at the end of each data feed, I get a paging array, that has a corresponding value in it. If it is -1 then there is no more data of that particular type. Else, the value indicates the number of entries of that type remaining that I can request in the next iteration of the call. – SAS Apr 12 '17 at 08:49
  • I have added and additional function which return the value of your object properties at https://jsfiddle.net/44fakLh7/4/, have a look. – warl0ck Apr 12 '17 at 09:05
  • This is great! thank you.. I had put together a messy if else statement, but this works great! – SAS Apr 12 '17 at 09:13
  • glad it helped, please accept the answer if it helped – warl0ck Apr 12 '17 at 09:15
  • I don't see any option to accept the answer. I upvoted it! how can I accept it? (New to StackOverflow) – SAS Apr 12 '17 at 09:26
  • there will be a tick just below the arrow from where you upvoted the answer. check http://stackoverflow.com/tour to know more. – warl0ck Apr 12 '17 at 09:32