猫鼬填充投掷错误

时间:2012-05-04 23:19:00

标签: node.js mongodb mongoose

以下代码应该(我认为)填充架构A中的dd字段但产生错误

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

});

错误:

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 

1 个答案:

答案 0 :(得分:2)

Schema.Objectid应为Schema.ObjectId:

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