title: Mongoose error: Schema hasn't been registered for model when populate
body: <p>I am trying to join two collections and being able to get the combined data. To do so using Mongoose, i am supposed to use the populate syntax to achieve that. I am receiving error that the Schema Schema hasn't been registered for 'User_Fb'. From my code, I have exported the models and required in my server.js but the error is still appearing. What have I done wrong?</p>
  
  <p>feed_post.model.js</p>
  
  <pre><code>var mongoose = require('mongoose');
  var conn_new_app     = mongoose.createConnection('mongodb://localhost/new_app');
  var User_fb = require('../models/fb_db.model');
  
  var Schema = mongoose.Schema;
  var feed_postSchema = new Schema({
      user_id:  { type: Schema.Types.ObjectId, ref: 'User_Fb' },
      content: String,
      location: String,
      image: [{ type : String }]
  });
  
  var Feed_Post = conn_new_app.model('Feed_Post', feed_postSchema);
  
  module.exports = Feed_Post;
  </code></pre>
  
  <p>fb_db.model.js</p>
  
  <pre><code>var mongoose = require('mongoose');
  var conn_new_app     = mongoose.createConnection('mongodb://localhost/new_app');
  var Feed_Post = require('../models/feed_post.model');
  
  var user_fb = new mongoose.Schema({
      name: String,
      location: String,
      fb_id: Number
  });
  
  var User_Fb = conn_new_app.model('User_Fb', user_fb);
  
  module.exports = User_Fb;
  </code></pre>
  
  <p>server.js</p>
  
  <pre><code>var express = require('express'),
  
  mongoose = require('mongoose'),
  User = require('./app/models/user.model'),
  Post = require('./app/models/post.model'),
  Maptest = require('./app/models/maptest.model'),
  Feed_Post = require('./app/models/feed_post.model'),
  User_Fb = require('./app/models/fb_db.model'),
  
  app = express();
  
  app.get('/testget', function(req,res){
      Feed_Post.findOne().populate('user_id').exec(function(err, c) {
          if (err) { return console.log(err); }
  
          console.log(c.fk_user.userName);
      });
  });
  </code></pre>
  
  <p><strong>UPDATED from Pier-Luc Gendreau Answer's</strong></p>
  
  <p>fb_db.model.js</p>
  
  <pre><code>module.exports = function (connection) {
      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
  
      var user_fb = new mongoose.Schema({
          name: String,
          location: String,
          fb_id: Number
      });
  
      return connection.model('User_FB', user_fb);;
  }
  </code></pre>
  
  <p>feed_post.model.js</p>
  
  <pre><code>module.exports = function (connection) {
      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
  
      var feed_postSchema = new Schema({
          user_id:  { type: Schema.Types.ObjectId, ref: 'User_Fb' },
          content: String,
          location: String,
          image: [{ type : String }]
      });
  
      return connection.model('Feed_Post', feed_postSchema);;
  }
  </code></pre>
  
  <p>server.js</p>
  
  <pre><code>var express = require('express'),
  app = express(),
  
  mongoose = require('mongoose'),
  conn_new_app = mongoose.createConnection('mongodb://localhost/new_app'),
  User_Fb = require('./app/models/fb_db.model')(conn_new_app),
  Feed_Post = require('./app/models/feed_post.model')(conn_new_app);
  
  app.get('/testget', function(req,res){
      Feed_Post.find().populate('user_id').exec(function(err, res) {
          if (err) { return console.log(err); }
          console.log(res);
      });
  });
  </code></pre>