title: mongoose findall return data from a different model
body: <p>In <strong>users.js</strong>:</p>
  
  <pre><code>var mongoose = require('mongoose');
  
  var User = mongoose.model('user', {
    username: {
      type: String,
      required: true,
      unique: true,
      lowercase: true,
    },  
    tasks: [{
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Tasks',
    }],
  });
  module.exports = User;
  </code></pre>
  
  <hr>
  
  <p>I then add a newUser named user1 and save to mongo.</p>
  
  <p>The mongo doc looks like:</p>
  
  <pre><code>{ 
      "_id" : ObjectId("574fb94f6a1e7d1826c16058"),
      "username" :  "user1",
      "tasks" : [ ],
      "__v" :  0
  }
  </code></pre>
  
  <hr>
  
  <p>then try to fetch the document and it works fine in this handler:</p>
  
  <p>In <strong>handlerA.js</strong>:</p>
  
  <pre><code>var User = require('../models/users.js');
  
  module.exports.getUser = function(req, res){
    User.findOne({username: "user1"}, function(err, data){
      if(err){
        console.log('getUser err', err);
        res.send('ERROR')
      } else {
        console.log('getUser fx success = ', err, data);
        res.send(data)
      }
    });
  };
  </code></pre>
  
  <p>The result of the <code>console.log</code>:</p>
  
  <blockquote>
    <p>getUser fx success =  null { tasks: [],  __v: 0,  username: 'user1',  _id: 574fb94f6a1e7d1826c16058 }</p>
  </blockquote>
  
  <hr>
  
  <p>but same code fails in this other handler in a separate file.</p>
  
  <p>In <strong>handlerB.js</strong>:</p>
  
  <pre><code>var User = require('../models/users.js');
  
  module.exports.addStuff = function(req, res){
     User.findOne({username: "user1"}, function(err, data){
      if(err){
        console.log('addStuff err', err);
        res.send('ERROR')
      } else {
        console.log('addStuff fx success =', err, data);
        res.send(data)
      }
    });
  };
  </code></pre>
  
  <p>The result of the <code>console.log</code>:</p>
  
  <blockquote>
    <p>addStuff fx success null null</p>
  </blockquote>
  
  <p>Tried....and Failed....</p>
  
  <p>I also tried this other solution from this question: <a href="https://stackoverflow.com/questions/11661590/mongoose-query-return-null">Mongoose query return null</a></p>
  
  <p>Mongoose pluralizes model names so it's running find on the "blogposts" collection instead of "blogpost". That said, your query in the mongo shell is on the "blogmodel" collection. In that case:</p>
  
  <pre><code>var BlogModel = mongoose.Model("BlogModel", ..)
  </code></pre>
  
  <p>or pass the collection name as the third param:</p>
  
  <pre><code>var BlogModel = mongoose.model("BlogPost", schema, "blogmodel")
  </code></pre>
  
  <p>This solution results in handlerA.js returning a null document as well as handlerB.js.</p>
  
  <p>Thanks for your time.  Much appreciated.</p>
  
  <hr>
  
  <p>ADDENDUM.
  I ran a find({}) under both the User and the Tasks models in <strong>handlerB.js</strong>
  The returned document IN BOTH cases is based on the Tasks model.
  see console.logs below for User.find({}) and Tasks.find({})
  the err is the null value then the data. </p>
  
  <p>How badly have I broken things?  How can a Model.find return data that is not even in the model? </p>
  
  <pre><code>User.find({},funtion(err, data) ____________________
  console.log of err  then data
  null 
  [ { subtasks: [],
      team: [ [Object] ],
      __v: 0,
      maintask: '1',
      _id: 574fce63d744cba421f750c1 
    },
    { subtasks: [],
      team: [ [Object] ],
      __v: 0,
      maintask: '2',
      _id: 574fce65d744cba421f750c2 
    }
  ]
  
  Tasks.find({},function(err, data) ___________________ 
  console.log of err then data
  null 
  [ { subtasks: [],
      team: [ [Object] ],
      __v: 0,
      maintask: '1',
      _id: 574fce63d744cba421f750c1 },
    { subtasks: [],
      team: [ [Object] ],
      __v: 0,
      maintask: '2',
      _id: 574fce65d744cba421f750c2 
    },
  ]
  </code></pre>
  
  <p>This is the Tasks model...
  <strong>tasks.js</strong></p>
  
  <pre><code>var mongoose = require('mongoose');
  
  var subtaskSchema = new mongoose.Schema({
     subtask: {
     type: String,
    },
    team: {
      type: Array,
      'defualt': [],
    },
    done: {
      type: Boolean,
      'defualt': false,
    },
  });
  
  var Tasks = mongoose.model('tasks', {
    maintask: {
      type: String,
    },
    subtasks: {
      type: [subtaskSchema],
    },
    team: {
      type: Array,
      'defualt': [],
    },
    done: {
      type: Boolean,
      'defualt': false,
    },
  });
  
  module.exports = Tasks;
  </code></pre>