title: ReferenceError: next is not defined
body: <p>I have Encomenda that has an array of Itens. Itens can have an array of itens.
  I have the following code:</p>
  
  <pre><code>const mongoose = require('mongoose');
  const Schema = mongoose.Schema;
  var idvalidator = require('mongoose-id-validator');
  
  let ItemSchema = new Schema({
      produtoId:Number,
      itens:[{type:Schema.Types.ObjectId, ref: 'Item'}]
  });
  
  function autoPopulateItens(next){
      this.populate('itens');
      next();
  }
  ItemSchema
      .pre('findOne',autoPopulateItens)
      .pre('find',autoPopulateItens);
  
  
  module.exports=mongoose.model('Item',ItemSchema);
  
  let EncomendaSchema= new Schema({
      itens:[ItemSchema]
  });
  
  module.exports=mongoose.model('Encomenda',EncomendaSchema);
  
  
  function log(data){
      console.log(JSON.stringify(data,undefined,2))
  }
  </code></pre>
  
  <p>And I'm trying to make a post request on postman to create an Encomenda with the following code:</p>
  
  <pre><code>{
      "itens":[{
          "produtoId":5,
          "itens":[{
              "produtoId":6,
              "itens":[]
          },{
              "produtoId":7,
              "itens":[]
          },{
              "produtoId":8,
              "itens":[]
          }]
      }
          ]
  }
  </code></pre>
  
  <p>But when I try to create an Encomenda and save it to the mongodb it gives me an error:</p>
  
  <pre><code>  CoreMongooseArray [ { itens: [], _id: 5bd9fde20c29e35f2c0ca74a, produtoId: 5 } ]
  ValidationError: Encomenda validation failed: itens.0.itens: Cast to Array failed for value "[ { produtoId: 6 },
    { produtoId: 7, itens: [] },
    { produtoId: 8, itens: [] } ]" at path "itens"
      at new ValidationError (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\error\validation.js:30:11)
      at model.Document.invalidate (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\document.js:2026:32)
      at EmbeddedDocument.invalidate (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\types\embedded.js:289:19)
      at EmbeddedDocument.$set (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\document.js:989:10)
      at EmbeddedDocument._handleIndex (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\document.js:773:14)
      at EmbeddedDocument.$set (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\document.js:719:22)
      at EmbeddedDocument.Document (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\document.js:115:12)
      at EmbeddedDocument [as constructor] (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\types\embedded.js:39:12)
      at new EmbeddedDocument (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\schema\documentarray.js:75:17)
      at DocumentArray.cast (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\schema\documentarray.js:334:22)
      at DocumentArray.SchemaType.applySetters (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\schematype.js:763:12)
      at model.$set (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\document.js:981:18)
      at model._handleIndex (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\document.js:773:14)
      at model.$set (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\document.js:719:22)
      at model.Document (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\document.js:115:12)
      at model.Model (D:\ISEP\3º\ARQSI\pr2\node_modules\mongoose\lib\model.js:90:12)
  </code></pre>
  
  <p>this how I try to create and save an Encomenda:</p>
  
  <pre><code>exports.encomenda_create = function (req, res,next) {
      var Client = require('node-rest-client').Client;
      var client = new Client();
  
       let encomenda=new Encomenda(req.body);
  
      encomenda.save(function (err) {
          if (err) {
              return next(err);
          }
          res.send('encomenda criada com sucesso')
      })
  };
  </code></pre>