title: How to loop through an array in mongodb?
body: <p>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.</p>
  
  <pre><code>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=== '' &amp;&amp; req.body.lastName==='' &amp;&amp; req.body.email==='' &amp;&amp; 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
                  });
              } 
          }
      });
  
  };
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>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.&lt;anonymous&gt; (C:\Users\Lior\Desktop\nodep\test5\routes\user.js:193
  :12)
      at Promise.&lt;anonymous&gt; (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
  </code></pre>