mrwooster
mrwooster

Reputation: 24207

Mongoose populate throwing error

The follow code should (i thought) populate the dd field in schema A but produces an error

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/test');

var A = new mongoose.Schema({
        dd : [{type : mongoose.Schema.Objectid, ref : 'D'}]
});

var D = new mongoose.Schema({
    a : String
});




var a = mongoose.model('A', A);
var d = mongoose.model('D', D);

var md = new d();

md.save(function(err) {
    if(err) console.log(err);
    ma = new a({dd : md._id});

    ma.save(function(err) {
        if(err) console.log(err);
        var qry = a.find({}).populate('dd').run(function(err, docs) {
            console.log(JSON.stringify(docs));
        });
    });

});

Error:

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
Error: Schema hasn't been registered for model "undefined".
Use mongoose.model(name, schema)
    at Mongoose.model (/mongoose/lib/index.js:173:13)
    at NativeConnection.model 

Upvotes: 0

Views: 2401

Answers (1)

aaronheckmann
aaronheckmann

Reputation: 10780

Schema.Objectid should be Schema.ObjectId:

var A = new mongoose.Schema({
        dd : [{type : mongoose.Schema.ObjectId, ref : 'D'}]
});

Upvotes: 2

Related Questions