Mongoose Populate in Node JS
Asked Answered
R

2

5

I've been trying to follow the information in Mongoose Population, but I'm getting the exception:

MissingSchemaError: Schema hasn't been registered for model "undefined".

The code I have goes like this:

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 on the populate, I've tweaked it a number of times to different answers found on forums, and I'm sure it will be something simple, but can someone point me in the right direction?

Cheers.

Roxannaroxanne answered 19/2, 2013 at 4:20 Comment(0)
D
9

In your schema definitions, I see that you have defined 'first_id' as an array in Second schema. Compared to a relational database, this will be like an one-to-many relationship in which the parent table is the Second collection, and First collection as the child. Then you're doing wrong trying to populate the second with the first.

Suppose I have a Users collection, and a Clients collection, in which each client has an user related to it. Then the code will be:

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 give you some point to help.

Decimate answered 19/2, 2013 at 6:4 Comment(1)
Many thanks, John! I must have copied in that part to make that ref field object in to an array from somewhere, or it was a typo, anyway - it's all fixed now, Cheers!Roxannaroxanne
W
0

I had the same error

Schema hadn't been registered for model "States".

Sometimes it might also be that the model you are trying to reference is not assigned to a schema.

like in the code below.

First: I was trying to reference the states model I created with States, but it was not the name of the model. Instead, the name was states

const 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

const State = mongoose.model( 'states', StateSchema ); // <= Typo here
module.exports = State ;

All I need to do was change the 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 }
});
Watchman answered 24/12, 2017 at 10:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.