omar.ite
omar.ite

Reputation: 15

$push in embedded array Meteor collection2

In a Meteor project , and I'm using [collection2 package] I have the following collection2 Schema:

  var schema =  new SimpleSchema ({
    comments: {
        type: [{text: String, createdAt: Date}],
        optional: true
     }})

And when I use this query in Meteor method :

Articles.update({_id: articleId}, {$push: {comments: {text: "yryd"}}})

It insert a blank object in comments array ... OK there is no problem in this query cause i run it in mongo terminal and all thing seems good and the insert operation done What is the problem in your opinion?

Upvotes: 0

Views: 435

Answers (1)

Blakes Seven
Blakes Seven

Reputation: 50406

Your schema basically appears to be incorrect for what you want to do here. It most likely needs to look something like this:

Articles new Meteor.collection("articles");

CommentSchema = new SimpleSchema({
    "text": { type: String },
    "createdAt": { type: Date, defaultValue: Date.now }
});

Articles.attachSchema(
    new SimpleSchema({
        "comments": [CommentsSchema]
    })
);

Then when you add in new things your schema types are verified for the "text" field being present, and fields like "createdAt" are added to the sub-document within the array entry automatically.

Upvotes: 1

Related Questions