3

This is my code to loop through the results for Users and then I am trying to loop through the friend request array made by the search from the user. However, I keep getting a TypeError: cannot read property '$elemMatch of undefined. I am using mongoose and Node.Js and this searchPost is in the route folder.

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=== '') {
                return res.render('searchError', {title: 'Weblio'});        
            } else {

                    for(x in users) {

                        User.findById(req.signedCookies.userid, 
                            {friendRequest: x.id}
                                if(x.id === true ) {
                                    console.log('addman1'); 
                                    return userAdd = false;
                                } else {
                                    console.log('addman2');
                                    return userAdd = true;
                                }

                            });

                        }       


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

};

Error:

TypeError: Cannot read property '$elemMatch' of undefined
    at Query._castFields (C:\Users\Lior\Desktop\nodep\test5\node_modules\mongoos
e\lib\query.js:2203:22)
    at Query.findOne (C:\Users\Lior\Desktop\nodep\test5\node_modules\mongoose\li
b\query.js:1775:25)
    at Function.findOne (C:\Users\Lior\Desktop\nodep\test5\node_modules\mongoose
\lib\model.js:944:16)
    at Function.findById (C:\Users\Lior\Desktop\nodep\test5\node_modules\mongoos
e\lib\model.js:882:15)
    at Promise.<anonymous> (C:\Users\Lior\Desktop\nodep\test5\routes\user.js:193
:12)
    at Promise.<anonymous> (C:\Users\Lior\Desktop\nodep\test5\node_modules\mongo
ose\node_modules\mpromise\lib\promise.js:162:8)
    at Promise.EventEmitter.emit (events.js:95:17)
    at Promise.emit (C:\Users\Lior\Desktop\nodep\test5\node_modules\mongoose\nod
e_modules\mpromise\lib\promise.js:79:38)
    at Promise.fulfill (C:\Users\Lior\Desktop\nodep\test5\node_modules\mongoose\
node_modules\mpromise\lib\promise.js:92:20)
    at C:\Users\Lior\Desktop\nodep\test5\node_modules\mongoose\lib\query.js:1725
:26
[ERROR] 17:23:30 TypeError
4
  • Could you please add your full error stack. That would be helpful. Commented Jul 21, 2013 at 6:51
  • I included the error on the bottom Commented Jul 21, 2013 at 21:25
  • I have to use x._id instead of x.id. Commented Jul 21, 2013 at 21:36
  • I tried that, it stil leaves me with the same error Commented Jul 21, 2013 at 22:19

2 Answers 2

5

I think you might be trying to do something like this

var totalDocsToProcess = users.length;

for(var i = 0; i < users.length; i++) {
  User.findById(req.signedCookies.userid, function(err, x) {
    totalDocsToProcess = totalDocsToProcess - 1;

    if(x.id === true ) {
      console.log('addman1'); 
      return userAdd = false;
    } else {
      console.log('addman2');
      return userAdd = true;      
    }

    if(totalDocsToProcess == 0) {
      return res.render('searchResults', {title: 'Weblio',        
        usersFound: users, 
        userAdded: userAdd
      });      
    }
  })
}
Sign up to request clarification or add additional context in comments.

Comments

2

for(x in users) x is the key of the array not the value, you should try:

for(var i = 0; i < users.length; ++i) { 
    var x = users[i]; etc etc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.