Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy mongoose.model('Model').Schema is going wrong?
    primarykey
    data
    text
    <p>I can't find what I'm doing wrong... I'm trying to define sub-docs in mongoose models, but when I split Schema definitions into another file, childrens model aren't respected.</p> <p>First, defining a Comment schema:</p> <pre><code>var CommentSchema = new Schema({ "text": { type: String }, "created_on": { type: Date, default: Date.now } }); mongoose.model('Comment', CommentSchema); </code></pre> <p>Next, creating another schema by loading from mongoose.model() (like we would loading it from another file</p> <pre><code>var CommentSchema2 = mongoose.model('Comment').Schema; </code></pre> <p>Now defining parent schema:</p> <pre><code>var PostSchema = new Schema({ "title": { type: String }, "content": { type: String }, "comments": [ CommentSchema ], "comments2": [ CommentSchema2 ] }); var Post = mongoose.model('Post', PostSchema); </code></pre> <p>And some tests</p> <pre><code>var post = new Post({ title: "Hey !", content: "nothing else matter" }); console.log(post.comments); // [] console.log(post.comments2); // [ ] // &lt;-- space difference post.comments.unshift({ text: 'JOHN' }); post.comments2.unshift({ text: 'MICHAEL' }); console.log(post.comments); // [{ text: 'JOHN', _id: 507cc0511ef63d7f0c000003, created_on: Tue Oct 16 2012 04:07:13 GMT+0200 (CEST) }] console.log(post.comments2); // [ [object Object] ] post.save(function(err, post){ post.comments.unshift({ text: 'DOE' }); post.comments2.unshift({ text: 'JONES' }); console.log(post.comments[0]); // { text: 'DOE', _id: 507cbecd71637fb30a000003, created_on: Tue Oct 16 2012 04:07:13 GMT+0200 (CEST) } // :-) console.log(post.comments2[0]); // { text: 'JONES' } // :'-( post.save(function (err, p) { if (err) return handleError(err) console.log(p); /* { __v: 1, title: 'Hey !', content: 'nothing else matter', _id: 507cc151326266ea0d000002, comments2: [ { text: 'JONES' }, { text: 'MICHAEL' } ], comments: [ { text: 'DOE', _id: 507cc151326266ea0d000004, created_on: Tue Oct 16 2012 04:07:13 GMT+0200 (CEST) }, { text: 'JOHN', _id: 507cc151326266ea0d000003, created_on: Tue Oct 16 2012 04:07:13 GMT+0200 (CEST) } ] } */ p.remove(); }); }); </code></pre> <p>As you can see, with CommentSchema, ids and default properties are correctly set. But with CommentSchema2, the loaded one, it's going wrong.</p> <p>I've tried to use a 'population' version but it's not what I'm looking for. I don't want to have to use another collection.</p> <p>Does anyone of you know what's wrong ? Thanks !</p> <p>mongoose v3.3.1</p> <p>nodejs v0.8.12</p> <p>Full gist: <a href="https://gist.github.com/de43219d01f0266d1adf" rel="nofollow">https://gist.github.com/de43219d01f0266d1adf</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload