Node.js 填充没有像我预期的那样工作

Node.js 填充没有像我预期的那样工作,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,请在下面找到我的代码 var mongoose = require('mongoose'); var util = require('util'); var db = mongoose.connect('mongodb://localhost:27017/prats', function(err) { if (err) throw err;} //this does not get printed ); mongoose.connection.on("open", function()

请在下面找到我的代码

var mongoose = require('mongoose');
var util = require('util');

var db = mongoose.connect('mongodb://localhost:27017/prats', function(err) {
  if (err) throw err;}  //this does not get printed
);

mongoose.connection.on("open", function(){
  console.log("mongodb is connected")}  //this gets printed
);

var Schema = mongoose.Schema;


var TestDocumentAccessSchema = new Schema({
  documentId: { type: Schema.ObjectId },
  userId: { type : String }, // Can be an SSO or a group (DL) id
  userName: { type : String },
});

var TestDocumentMasterSchema = new Schema({
  documentId: { type: Schema.ObjectId, ref: 'TestDocumentAccess'},
  masterId: { type: Schema.ObjectId }
});

var TestDocAccess  = mongoose.model('TestDocumentAccess', TestDocumentAccessSchema);
var TestDocMaster = mongoose.model('TestDocumentMaster', TestDocumentMasterSchema);

var document = new TestDocAccess(
    { documentId: '50dc37d6022b2bdd07000004',
    userId : "1234",
    userName : "Test Name",
    }
);

document.save(function (err) {
  if (err) return handleError(err);

  var master = new TestDocMaster({
    documentId: "50dc37d6022b2bdd07000004",
    masterId: "50a5e7bcda3c4d557f00847a"    // assign an ObjectId
  });

  master.save(function (err) {
    if (err) return handleError(err);
        console.log("That's it save success!!....");

    TestDocMaster.find({'masterId': '50a5e7bcda3c4d557f00847a'})
    .populate('documentId')
    .exec(function(err, TestDocAccess) {
        //I want all the document row corresponding to the master Id
        console.log("=========Test Doc Master======" +util.inspect(TestDocAccess.documentId));
    });
  });
}) 
我有多个与主id相对应的documentId,我希望TestDocAccess.documentId中包含所有文档详细信息,但我得到了一个未定义的

请指出上面代码中的错误。

对于
填充('documentId')
要在您的示例中工作,引用的
TestDocAccess
模型集合中必须有一个文档,其id为
\u id
(而不是您期望的
documentId
)与
TestDocMaster
模型集合中的
documentId
匹配