填充多个级别不起作用

Populate multiple level does not work

First = new mongoose.Schema({
  name: String,
  second: {type: Schema.Types.ObjectId, ref: 'Second'},
});

Second = new mongoose.Schema({
  name: String,
  third: {type: Schema.Types.ObjectId, ref: 'Third'},
});


Third = new mongoose.Schema({
  name: String
});

First.find({}).populate({
  path: 'Second',
  populate: { path:  'Third'}
  }).exec(function(err, result) {
    console.log(result)
    })

第一个填充没问题,但第三个总是 null。意思是我得到了这样的东西:

{
  name: 1,
  second: {
    name: 2,
    third: null

    }}
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

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

var FirstSchema = new mongoose.Schema({
  name: String,
  second: {type: Schema.Types.ObjectId, ref: 'Second'},
});

var SecondSchema = new mongoose.Schema({
  name: String,
  third: {type: Schema.Types.ObjectId, ref: 'Third'},
});


var ThirdSchema = new mongoose.Schema({
  name: String
});

var First = mongoose.model('First', FirstSchema);
var Second = mongoose.model('Second', SecondSchema);
var Third = mongoose.model('Third', ThirdSchema);

First.remove({}).exec();
Second.remove({}).exec();
Third.remove({}).exec();

var _3 = new Third({name: 'third'});
_3.save(function(err1) {
  if (err1) {
    throw err1;
  }
  var _2 = new Second({name: 'second', third: _3.id});
  _2.save(function(err2) {
    if (err2) {
      throw err2;
    }
    var _1 = new First({name: 'first', second: _2.id});

    _1.save(function() {
      First.find({}).populate({
        path: 'second',
        populate: { path:  'third'}
      }).exec(function(err, result) {
        console.log(result[0]);
      });
    });
  });
});

您的 path 是否被分配了错误的值或其他什么?

应该是字段名,不是对象名