title: mongoose .create give node.js "ValidationError: validation failed: Cast to [undefined] failed for value..."
body: <p>I have an App and I want to push a comment to a single id page (showing a campground) passing a value in an array. I use to express and mongoose.</p>
  
  <p>Error</p>
  
  <blockquote>
    <p>UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1):
    ValidationError: validation failed: Cast to [undefined] failed for
    value...</p>
  </blockquote>
  
  <p>This is the model of the page to show:</p>
  
  <p><strong>models/campground.js</strong></p>
  
  <pre><code>var mongoose = require("mongoose");
  
  var campgroundSchema = new mongoose.Schema({
      name: String,
      image: String,
      description: String,
      comments: [{
          type: mongoose.Schema.Types.ObjectId,
          ref: "Comment"
      }]
  });
  
  module.exports = mongoose.model("Campground", campgroundSchema);
  </code></pre>
  
  <p>The campgroundSchema refers to the Comment model:</p>
  
  <p><strong>models/comment.js</strong></p>
  
  <pre><code>var mongoose = require("mongoose");
  
  var commentSchema = new mongoose.Schema({
      text: String,
      author: String
  });
  
  module.exports = mongoose.model("Comment", commentSchema);
  </code></pre>
  
  <p>Both are used in the Create comment route, that find a campground by id, then create a comment retrieving data from a form, the trying to push that comment into the comments array of the previously founded campground, and save:</p>
  
  <p><strong>app.js</strong></p>
  
  <pre><code>app.post("/campgrounds/:id/comments", function (req, res) {
      Campground.findById(req.params.id, function (err, campground) {
          if (err) {
              console.log(err);
              res.redirect("/campgrounds");
          } else {
              Comment.create(req.body.comment, function (err, comment) {
                  if (err) {
                      console.log(err);
                  } else {
                      campground.comments.push(comment);
                      campground.save();
                      res.redirect("/campgrounds/" + campground._id);
                      console.log(req.body.comment);
                      }
              });
  
          }
      });
  });
  </code></pre>
  
  <p>This is the code of the "add new comment" form:</p>
  
  <p><strong>comments/new.ejs</strong></p>
  
  <pre><code>&lt;div class="container"&gt;
      &lt;div class="row"&gt;
          &lt;h1 style="text-align: center;"&gt;Add a new Comment to &lt;%= campground.name %&gt;&lt;/h1&gt;
          &lt;div style="width: 30%; margin:25px auto;"&gt;
              &lt;form action="/campgrounds/&lt;%= campground._id %&gt;/comments" method="POST"&gt;
                  &lt;div class="form-group"&gt;
                      &lt;input class="form-control" type="text" name="comment[text]" placeholder="text"&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;input class="form-control"  type="text" name="comment[author]" placeholder="author"&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;button class="btn btn-lg btn-primary btn-block"&gt;Submit!&lt;/button&gt;
                  &lt;/div&gt;
              &lt;/form&gt;
              &lt;a href="/campgrounds"&gt;Go Back&lt;/a&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>and this is the "show campground" page:</p>
  
  <p><strong>campgrounds/show.ejs</strong></p>
  
  <pre><code>&lt;div class="container"&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-md-3 col-sm-6"&gt;
              &lt;h1&gt;&lt;%= campground.name %&gt;&lt;/h1&gt;
              &lt;div class="..."&gt;
                  &lt;img src="&lt;%= campground.image %&gt;" class="media-object"&gt;
                  &lt;p&gt;&lt;%= campground.description %&gt;&lt;/p&gt;
                  &lt;p&gt;&lt;a class="btn btn-success" href="/campgrounds/&lt;%= campground._id %&gt;/comments/new"&gt;Add New Comment&lt;/a&gt;&lt;/p&gt;
                  &lt;% campground.comments.forEach(function(comment) {%&gt;
                      &lt;p&gt;&lt;strong&gt;&lt;%= comment.text %&gt;&lt;/strong&gt; - &lt;%= comment.author %&gt;&lt;/p&gt;
                  &lt;% })%&gt;
                  &lt;a href="/campgrounds"&gt;Go Back&lt;/a&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>The route redirect correctly to the "show campground" page, but no new comment is added to the loop. I have a Node Warning in console:</p>
  
  <pre><code>(node:1940) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): 
  ValidationError: Campground validation failed: comments: Cast to [undefined] failed for value 
  "[{
  "_id":"5a55f5afd9bf5c0794ca7220",
  "text":"previously existing comment",
  "author":"first author",
  "__v":0
  }]" 
  at path "comments"
  </code></pre>
  
  <p>Please note that the comment logged is the existing comment, the new one submitted by the form is only logged because of <code>console.log(req.body.comment)</code> an example:</p>
  
  <pre><code>{ text: 'lortem ipsum', author: 'nick beer' }
  </code></pre>
  
  <p>How can I solve this problem?</p>
  
  <p><strong>EDIT</strong>: thanks to JavaEvgen, I try nested schema and the app works without changing other lines. But seems I cannot work with referenced schemas. Where is the Error?</p>