title: MEAN stack: first object pushed into MongoDB schema's array causes RangeError
body: <p>edit: I have realized that this is hanging at the line "res.json(comment);" of my angular-ui-route. Still not sure why.</p>
  
  <p>I'm following through the tutorial here: <a href="https://thinkster.io/tutorials/mean-stack" rel="nofollow noreferrer">https://thinkster.io/tutorials/mean-stack</a>. This may be related to the fact that the tutorial is a little outdated. I've uploaded my code in it's current state to <a href="http://github.com/orieporter/flappernews" rel="nofollow noreferrer">http://github.com/orieporter/flappernews</a></p>
  
  <p>It's a simple reddit clone web app for posting links, comments, and upvoting them. I am getting a "RangeError: Maximum call stack size exceeded" error each time I add the first comment to a post. Subsequent comments work no problem. When I restart my node server and reload the page the comment that broke the server is there. Can anyone help me find the problem here?</p>
  
  <p>Full error:</p>
  
  <pre><code>events.js:160
    throw er; // Unhandled 'error' event
    RangeError: Maximum call stack size exceeded
  at model.Document.$toObject (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\document.js:1962:24)
  at model.Document.toJSON (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\document.js:2300:15)
  at clone (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\utils.js:252:18)
  at cloneArray (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\utils.js:362:14)
  at clone (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\utils.js:247:12)
  at cloneObject (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\utils.js:343:13)
  at clone (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\utils.js:260:16)
  at model.Document.$toObject (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\document.js:2009:13)
  at model.Document.toJSON (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\document.js:2300:15)
  at clone (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\utils.js:252:18)
  at cloneObject (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\utils.js:343:13)
  at clone (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\utils.js:260:16)
  at model.Document.$toObject (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\document.js:2009:13)
  at model.Document.toJSON (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\document.js:2300:15)
  at clone (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\utils.js:252:18)
  at cloneArray (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\utils.js:362:14)
  at clone (c:\Users\source\repos\flapper-news\node_modules\mongoose\lib\utils.js:247:12)
  </code></pre>
  
  <p>Relevant HTML:</p>
  
  <pre><code>&lt;form ng-submit="addComment()" style="margin-top:30px;"&gt;
      &lt;h3&gt;Add a new comment&lt;/h3&gt;
      &lt;div class="form-group"&gt;
          &lt;input type="text" class="form-control" placeholder="Comment" ng-model="body"&gt;&lt;/input&gt;
      &lt;/div&gt;
      &lt;button type="submit" class="btn btn-primary"&gt;Post&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Angular Controller Function:</p>
  
  <pre><code>$scope.addComment = function () {
      if ($scope.body === '') { return; }
      posts.addComment(post._id, {
          body: $scope.body,
          author: 'user'
      },
      $scope.post);
      $scope.body = '';
  };
  </code></pre>
  
  <p>Angular Factory Function:</p>
  
  <pre><code>o.addComment = function (id, comment, post) {
      return $http.post('/posts/' + id + '/comments', comment).then(function (response) {
          post.comments.push(response.data);
          return response.data;
      });
  };
  </code></pre>
  
  <p>Relevant Express Route:</p>
  
  <pre><code>router.post('/posts/:post/comments', function (req, res, next) {
      var comment = new Comment(req.body);
      comment.post = req.post;
  
      comment.save(function (err, comment) {
          if (err) { return next(err); }
  
          req.post.comments.push(comment);
          req.post.save(function (err, post) {
              if (err) { return next(err); }
  
              res.json(comment);
          });
      });
  });
  </code></pre>
  
  <p>Comment Schema:</p>
  
  <pre><code>var CommentSchema = new mongoose.Schema({
      body: String,
      author: String,
      upvotes: {type: Number, default: 0},
      post: {type: mongoose.Schema.Types.ObjectId, ref: 'Post' }
  });
  </code></pre>
  
  <p>Post Schema:</p>
  
  <pre><code>var PostSchema = new mongoose.Schema({
      title: String,
      link: String,
      upvotes: {type: Number, default: 0},
      comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }]
  });
  </code></pre>