title: Mongoose - Self-referential deep populate error
body: <p>I'm trying to populate a self-referential model a few times recursively. Here's my schema:</p>
  
  <pre><code>var TestSchema = new Schema({
    title: { type: String },
    counter: { type: Number },
    children: [ { type: Schema.Types.ObjectId, ref: 'test' } ]
  });
  var models = {
    Test: mongoose.model('test', TestSchema)
  };
  </code></pre>
  
  <p>So far, this functioning code populates everything one level:</p>
  
  <pre><code>models.Test.find().populate('children').exec(function(err, doc) {
    if (err)
      res.send(err);
    else {
      res.send(doc);
    }     });
  </code></pre>
  
  <p>But when I try to do something like:</p>
  
  <pre><code>models.Test.find().populate('children').populate('children.children').exec(function(err, doc) {
  </code></pre>
  
  <p>or even:</p>
  
  <pre><code>models.Test.find().populate('children').exec(function(err, doc) {
    if (err)
      res.send(err);
    else {
      models.Test.populate(doc, 'children.children', function(err, doc) {
         res.send(doc);
      });
    }
  });
  </code></pre>
  
  <p>I get this error: </p>
  
  <pre><code>TypeError: Cannot call method 'path' of undefined
      at search (..../api/node_modules/mongoose/lib/model.js:2059:28)
      at search (..../api/node_modules/mongoose/lib/model.js:2078:22)
      at Function._getSchema (..../api/node_modules/mongoose/lib/model.js:2085:5)
      at populate (..../api/node_modules/mongoose/lib/model.js:1706:22)
      at Function.Model.populate (..../api/node_modules/mongoose/lib/model.js:1686:5)
      at Promise.&lt;anonymous&gt; (..../api/api.js:22:19)
      at Promise.&lt;anonymous&gt; (..../api/node_modules/mongoose/node_modules/mpromise/lib/promise.js:162:8)
      at Promise.EventEmitter.emit (events.js:95:17)
      at Promise.emit (..../api/node_modules/mongoose/node_modules/mpromise/lib/promise.js:79:38)
      at Promise.fulfill (..../api/node_modules/mongoose/node_modules/mpromise/lib/promise.js:92:20)
  </code></pre>
  
  <p>The <a href="https://github.com/LearnBoost/mongoose/wiki/3.6-Release-Notes#added-modelpopulatedocs-opts-cb" rel="nofollow">mongoose 3.6 release notes</a> say that deep populates are allowed using Model.populate, but that's giving me an error. Does anyone know what's going on?</p>