Mongoose Population in Node JS

I tried to track information in Mongoose Population , but I am getting an exception:

MissingSchemaError: Schema was not registered for model "undefined".

The code I have is as follows:

mongoose = require('mongoose');
Schema = mongoose.Schema;
mongoose.connect(MONGO_SERVER);
ObjectId = Schema.ObjectId;

var FirstSchema = new Schema({
    label       : String
});
var SecondSchema = new Schema({
    first_id           : [{ type: mongoose.Schema.ObjectId, ref: 'First' }],
    type           : String,
    ...
});
var first= mongoose.model('First', FirstSchema);
var second= mongoose.model('Second', SecondSchema);

function test() {
    ...
    second.find({}).populate('first_id').exec(function(err,data){return true;});
    ...
}

      

And the error occurs when filling it in, I've tweaked it several times to the various answers found on the forums and I'm sure it'll be something simple, but can anyone point me in the right direction?

Greetings.

+3


source to share


2 answers


In your schema definitions, I see that you defined 'first_id' as an array in the second schema. Compared to a relational database, this looks like a one-to-many relationship with the parent table as the second collection and the first as the child. Then you are wrong trying to populate the second with the first.

Suppose I have a Users collection and a clients collection where each client has a user associated with it. Then the code will look like this:



var mongoose = require('mongoose');
mongoose.connect('mongodb://userName:password@server:port/dbname');
var conn = mongoose.connection;
conn.on('error', console.error.bind(console, 'connection error:'));
conn.once('open', function callback () {
    console.log('connected ');
});

var user = mongoose.Schema({
    userName: String
});

var client = mongoose.Schema({
    fk_user: { type: mongoose.Schema.ObjectId, ref: 'Users' },
    name: String
});

var UserModel = mongoose.model('Users', user);
var ClientModel = mongoose.model('Clients', client);

ClientModel.findOne().populate('fk_user').exec(function(err, c) {
    if (err) { return console.log(err); }

    console.log(c.fk_user.userName);
});

      

Hope this helps you.

+6


source


i had the same error

The schema has not been registered for State models.

sometimes it could also be that the model you are trying to reference is not schema-bound.

as in below code

firstly: I tried to reference a state model created with states, but that was not the name of the model, it was state instead



var CountriesSchema = new Schema({

name:{ type:String,required:true},
capital:{type:String},
description:{type:String},
cord:{

    latitude:{type:Number},
    longitude:{type:Number}

},
state:[{type:Schema.Types.ObjectId , ref:'States'}],
date_created:{type:Date},
date_modeified:{type:Date}



});

      

Meanwhile, the real name of the model was states

 var State = mongoose.model('states',StateSchema) ;

module.exports = State ;

      

All I did was change states to states

var CountriesSchema = new Schema({

    name:{ type:String,required:true},
    capital:{type:String},
    description:{type:String},
    cord:{

        latitude:{type:Number},
        longitude:{type:Number}

    },
    state:[{type:Schema.Types.ObjectId , ref:'states'}],
    date_created:{type:Date},
    date_modeified:{type:Date}



});

      

0


source







All Articles