0

Hey so I am using an async series to create an array with the user found in a search result to loop through and see if they are part of the friend request array of the user logged in. However, for some reason it is always ending up as false, does anyone notice anything wrong with the code below?

Here is the code for the route file:

exports.searchPost = function(req, res, err) {
    User.find({$or:[
            {firstName: req.body.firstName},
            {lastName: req.body.lastName},
            {email: req.body.email},
            {phone: req.body.phone}]
    }, function(err, users, userAdd) {
        if(err) {

            return res.render('searchError', {title: 'Weblio'}); 
        } else {

            if(req.body.firstName=== '' && req.body.lastName==='' && req.body.email==='' && req.body.phone=== '') {
                //maybe  a diff page saying that is not a valid search page
                return res.render('searchError', {title: 'Weblio'});        
            } else {


                    var userAdd,
                    series = [];

                    console.log('addman');
                    users.forEach(function (user) {
                        series.push(function (callback) {
                            User.findById(req.signedCookies.userid, {
                                friendRequest: user._id
                            }, function () {
                                if (user._id === true) {
                                console.log('addman1');
                                return callback(null, [user, true]);
                                } else {
                                    console.log('addman2');
                                    console.log(user._id);
                                    return callback(null, [user, false]);
                                }
                            });
                        });
                    });

                async.series(series, function (err, results) {
                //results should be an in order array of values;
                console.log(results);
            return res.render('searchResults', {title: 'Weblio',         
                    userAdded: results
                });
            }); 

            }

        }
    });

};  
Lion789
  • 4,402
  • 12
  • 58
  • 96

1 Answers1

1

I think there are a couple of issues where you check if (user._id === true) {.

The first issue is that your expecting user._id to be a Boolean - which it probably isn't. A simple if (user._id) { will check if it's a "truthy" value - which all numbers except 0 are.

However, I think there may be a second issue there: that user variable is from your users.forEach(function (user) { - I'm guessing that you actually want to be checking the results of your User.findById(req.signedCookies.userid, {friendRequest: user._id} function(... code. In that case you need to add error, user2 parameters to the callback function or something along those lines.

Nathan Friedly
  • 7,837
  • 3
  • 42
  • 59
  • user2 like this User.findById(req.signedCookies.userid, { friendRequest: user._id }, function (user2) { Or are you referring to replacing the user in the callback function to user2? – Lion789 Jul 22 '13 at 20:36
  • Just like you said, `function(user2) {` and then `if (user2._id) { ...` – Nathan Friedly Jul 22 '13 at 20:46
  • getting TypeError cannot read property '_id' of null for that line of if (user2._id) { – Lion789 Jul 22 '13 at 21:48
  • Before the `if` statement, call `console.dir(arguments)` - that should tell you what you're working with. I think the first arg is actually for errors and the second is the data. Sorry about that, I'll edit my answer. – Nathan Friedly Jul 23 '13 at 03:07
  • Ok I understand that, and that is correct but what is the second argument, how do I call the data I queried? – Lion789 Jul 23 '13 at 03:28
  • What db library are you using? (Run `npm ls` from the project directory.) And what's your overall goal with this code? – Nathan Friedly Jul 23 '13 at 13:44
  • Hey thanks nathan but I figured it out, using a much simpler method even http://stackoverflow.com/questions/17815251/loop-is-not-outputting-true-or-false-based-on-query – Lion789 Jul 23 '13 at 16:00