简体   繁体   English

填充多个级别不起作用

[英]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)
    })

First populate is ok, but Third is always null .第一个填充没问题,但第三个总是null Meaning I got some thing like this:这意味着我得到了这样的东西:

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

在此处输入图片说明

is there your path is assigned by a wrong value or something?你的path是否被错误的值或其他东西分配了?

it should be a field name, not a object name它应该是一个字段名,而不是一个对象名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM