Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose error: Schema hasn't been registered for model when populate

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?

feed_post.model.js

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;

fb_db.model.js

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;

server.js

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);
    });
});

UPDATED from Pier-Luc Gendreau Answer's

fb_db.model.js

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);;
}

feed_post.model.js

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);;
}

server.js

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);
    });
});
like image 694
Gene Lim Avatar asked Oct 12 '15 02:10

Gene Lim


People also ask

How does populate work Mongoose?

Mongoose Populate() Method. In MongoDB, Population is the process of replacing the specified path in the document of one collection with the actual document from the other collection.

Can I use Mongoose without schema?

To use Mongoose without defining a schema, we can define a field with the Mixed data type. const Any = new Schema({ any: Schema. Types. Mixed });

What is the difference between schema and model in Mongoose?

A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.

Is it mandatory to use Mongoose with Node application?

It's not mandatory to use Mongoose over the MongoDB Native API. However, there are some benefits to doing so.


2 Answers

This is all I had to do Customer.findOne({}).populate({ path: 'created_by', model: User }) instead of this Category.findOne({}).populate({'author'})

like image 125
Geofrey Asiimwe Avatar answered Sep 23 '22 21:09

Geofrey Asiimwe


You can get the field you want by using select

Example to get the email of the customer:

Customer.findOne({}).populate({ path: 'created_by', model: User, select: 'email' })

More Details in mongoose documentation

like image 43
Nawfel Hamdi Avatar answered Sep 25 '22 21:09

Nawfel Hamdi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!