Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7878285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:36:55+00:00 2026-06-03T03:36:55+00:00

I am building an API in node.js which uses mongodb and mongoose. Currently I

  • 0

I am building an API in node.js which uses mongodb and mongoose. Currently I have an embedded document within an embedded document (Schema within a Schema) that is simply not persisted to the database, and I have tried all I can but no luck.

I have the Schema’s defined in mongoose as:

var BlogPostSchema = new Schema({
  creationTime: { type: Date, default: Date.now },
  author: { type: ObjectId, ref: "User" },
  title: { type: String },
  body: { type: String },
  comments: [CommentSchema]
});

var CommentSchema = new Schema({
  creationTime: { type: Date, default: Date.now },
  user: { type: ObjectId, ref: "User" },
  body: { type: String, default: "" },
  subComments: [SubCommentSchema]
});

var SubCommentSchema = new Schema({
  creationTime: { type: Date, default: Date.now },
  user: { type: ObjectId, ref: "User" },
  body: { type: String, default: "" }
});

And the code I execute is as follows:

// Create a comment
app.post("/posts/:id/comments", function(req, res, next) {
  Posts.find({ _id : req.params.id }, function(err, item){
    if(err) return next("Error finding blog post.");                
    item[0].comments.push(new Comment(JSON.parse(req.body)));
    item[0].save(); // <= This actually saves and works fine
    respond(req, res, item[0].comments, next);
  });
});

// Create a subcomment
app.post("/posts/:id/comments/:commentid/subcomments", function(req, res, next) {
  Posts.find({ _id : req.params.id }, function(err, item){
    if(err) return next("Error finding blog post.");
    item[0].comments[req.params.commentid - 1].subcomments.push(new SubComment(JSON.parse(req.body)));
    item[0].save(); // <= This completes (without error btw) but does not persist to the database
    respond(req, res, item[0].comments[req.params.commentid - 1].subcomments, next);
  });
});

I can create Blog Posts with comments without problem, but for some reason I can not create subcomments on a comment. The Blog Post document actually has the comments and subcomments attached when printing to the console during execution – only it does not save to the database (it saves the Blog Post with a comment, but no subcomments).

I have tried to “markModified” on the comments array, but no change:

Posts.markModified("comments"); // <= no error, but also no change
...
Posts.comments.markModified("subcomments"); // <= produces an error: "TypeError: Object [object Object] has no method 'markModified'"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:36:57+00:00Added an answer on June 3, 2026 at 3:36 am

    Problem since solved. I was given the answer by Aaron Heckmann over on the mongoose Google Group:

    Always declare your child schemas before passing them to you parent schemas otherwise you are passing undefined.

    SubCommentSchema should be first, then Comment followed by BlogPost.

    After reversing the schemas it worked.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building an application that uses an elaborate API to fetch data from Youtube,
I am building an app that uses the fine Urban Airship api to send
Ok Im building AN API but also wanting to have that API used by
I'm building a node.js application with Mongoose and have a problem related to sorting
I am currently building an API which will be used by a webservice. I
I have been building an API which returns XML code. For example, the API
I'm building an api that uses OAuth2. I've successfully unit tested all of the
I have created an API in AS3 that uses Flex bindings. The API is
I am building an API which I intend to upgrade over time. Is it
I'm building a REST API, but I've encountered a problem. It seems that accepted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.