title: Use value in mongoose scope function
body: <p>I'd like to find mails of a user by his id. But i have a problem because mongoose function is async.</p>
  
  <pre><code>Mail.find({receiver_id: '#id#'}, function(err, mails) {
      var result = [];
      var count = 0;
      if (mails.length &gt; 0) {
          for (m of mails) {
              User.findById(m.sender_id, function(err, user) {
                  if (user) {
                      result.push({
                          id: m._id,
                          title: m.title,
                          body: m.body
                      });
                      if (++count == mails.length) {
                          res.json({success: true, data: result});
                      }
                  }
              });
          }
      } else {
          res.json({success: false, error: 'Cannot find any email'});
      }
  });
  </code></pre>
  
  <p>The problem is, when i got my result, all mails were the same. As i understood, because User.findById is an async function, so when i use <strong>m</strong> in that function, the <strong>m</strong> always represents the last child of <strong>mails</strong>.</p>
  
  <p>I've figured out that if i pass some variables as the second param of findById function, it should work:</p>
  
  <pre><code>User.findById(m.sender_id, {id: m._id, title: m.title, body: m.body}, function(err, user) {
      if (user) {
          result.push({id, title, body});
          if (++count == mails.length) {
              res.json({success: true, data: result});
          }
      }
  });
  </code></pre>
  
  <p>but because my document doesn't have only three fields, so i think there's another better solution.</p>
  
  <p>How can i get it right ? Thanks !</p>