0

以下代码应该(我认为)填充模式 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 
4

1 回答 1

2

Schema.Objectid 应该是 Schema.ObjectId:

var A = new mongoose.Schema({
        dd : [{type : mongoose.Schema.ObjectId, ref : 'D'}]
});
于 2012-05-07T15:41:57.957 回答