title: How to add object to nested Array using Node.js and Mongoose
body: <p>How can I add object to my nested Array in <code>PartnerSchema</code>? </p>
  
  <p>I separate documents, because in the future there will be more of nested arrays.</p>
  
  <p>This is my schema:</p>
  
  <pre><code>var productSchema = new mongoose.Schema({
      name: String
  });
  
  var partnerSchema = new mongoose.Schema({
      name: String,
      products: [
          {
              type: mongoose.Schema.Types.ObjectId,
              ref: 'Product'
          }]
  });
  
  module.exports = {
      Partner: mongoose.model('Partner', partnerSchema),
      Product: mongoose.model('Product', productSchema)
  }
  </code></pre>
  
  <p>And this is my backend:</p>
  
  <pre><code>var campSchema = require('../model/camp-schema');
  
  router.post('/addPartner', function (req, res) {
      new campSchema.Partner({ name : req.body.name }).save(function (err, response) {
          if (err) console.log(err);
          res.json(response);
      });
  });
  
  router.post('/addProduct', function (req, res) {
      campSchema.Partner.findByIdAndUpdate({ _id: req.body.partnerId }, 
          {
          $push: {
              "products": {
                  name: req.body.dataProduct.name
              }
          }
      }, { safe: true }, function (err, response) {
          if (err) throw err;
          res.json(response);
      });
  });
  </code></pre>
  
  <p>I can add Partner by using <code>/addPartner</code> and it works fine.</p>
  
  <p>Problem is with second function <code>/addProduct</code> I can't add Product to Array in Partner Schema. I have an error: <code>CastError: Cast to undefinded failed for value "[object Object]" at path "products"</code></p>