Skip to content Skip to sidebar Skip to footer

Loop Is Not Outputting True Or False Based On Query

please take a look and let me know why the loop's output is not correct? Basically I am looping through the friendId array of a user and through the user results for a search and s

Solution 1:

One problem you have is the location where you are attempting to log the result of isFriend. It's currently being logged just after you set isFriend to false as you enter the search loop.

That statement needs to be moved just after the inner for loop.

Without having all of the inputs, it makes it very difficult to guess where the problem might be.

Using the following as input (Can you provide what the actual input is in JSON format?):

var users = [
    {
        lastName: 'Smith',
        '_id': "51ee2017c2023cc816000001"
    },
    {
        lastName: 'Jones',
        '_id': "51ee2017c2023cc816000002"
    },
    {
        lastName: 'Macks',
        '_id': "51ee2017c2023cc816000003"
    }
];

var signedInUser = {
    friendRequest: [{
        "friendId": "51ee2017c2023cc816000002",
        "read": 0,
        "date_requested": "2013-07-23T06:29:39.021Z"
    }, {
        "friendId": "51ee203cc2023cc816000003",
        "read": 0,
        "date_requested": "2013-07-23T06:42:37.872Z"
    }]
};

function test(err, signedInUser) {
    console.log("\nsignedInUser.friendRequest\n" + JSON.stringify(signedInUser.friendRequest, null, 2));
    for (var x = 0; x < users.length; x++) {
        users[x].isFriend = false;
        console.log("\n" + users[x].lastName);
        for (var i = 0; i < signedInUser.friendRequest.length; i++) {
            console.log(users[x]._id + ' - ' + signedInUser.friendRequest[i].friendId);
            if (users[x]._id === signedInUser.friendRequest[i].friendId) {
                users[x].isFriend = true;
                console.log('test');
                break;
            }
        }
        console.log(users[x].isFriend);
    }

    console.log("\nFinal users:\n" + JSON.stringify(users, null, 2));
}

test(null, signedInUser);

I see the following results:

signedInUser.friendRequest
[
  {
    "friendId": "51ee2017c2023cc816000002",
    "read": 0,
    "date_requested": "2013-07-23T06:29:39.021Z"
  },
  {
    "friendId": "51ee203cc2023cc816000003",
    "read": 0,
    "date_requested": "2013-07-23T06:42:37.872Z"
  }
]

Smith
51ee2017c2023cc816000001 - 51ee2017c2023cc816000002
51ee2017c2023cc816000001 - 51ee203cc2023cc816000003
false

Jones
51ee2017c2023cc816000002 - 51ee2017c2023cc816000002
test
true

Macks
51ee2017c2023cc816000003 - 51ee2017c2023cc816000002
51ee2017c2023cc816000003 - 51ee203cc2023cc816000003
false

Final users:
[
  {
    "lastName": "Smith",
    "_id": "51ee2017c2023cc816000001",
    "isFriend": false
  },
  {
    "lastName": "Jones",
    "_id": "51ee2017c2023cc816000002",
    "isFriend": true
  },
  {
    "lastName": "Macks",
    "_id": "51ee2017c2023cc816000003",
    "isFriend": false
  }
]

Other than the log statement being in the wrong place (I don't think I changed the semantics of your code), with this set of inputs, the logic works. It is likely the input you were expecting is not what you are receiving.

It turns out the OP was using the mongoose native driver for nodejs and, after researching found the answer to the comparison portion of the problem here: Comparing mongoose _id and strings


Solution 2:


Solution 3:

This is a wild guess, but I suspect you are attempting to return the value here:

res.render('searchResults', {title: 'Weblio', userAdded: users });

If so, user added will have the users collection under userAdded, not a true/false.

You might need to set a boolean value to true (e.g. myVariable = true) when you break of the loop and use that variable as your return value.


Post a Comment for "Loop Is Not Outputting True Or False Based On Query"