3

The idea is to push an object that looks like this into a field called likes, which is an array:

{
  movieId: "VgtyvjVUAjf8ya",
  information: {
                 genre: "Action",
                 length: "160",
                 language: "English"
               }
}

I thought this would do it:

Meteor.users.update({_id: Meteor.userId()}, {$push: {likes: {movieId: movieId, information: informationObj}}})

But either it is wrong or the validation by SimpleSchema has some issues (it doesn't complain, though) because all I get is an empty object in an array! And no, there's nothing wrong with the values themselves, I have checked.

The SimpleSchema for the field in question looks like this:

likes: {
            type: [Object],
            optional: true
        }

I've tried reading through the documentation but I don't really understand what's wrong. Anyone knows?

1 Answer 1

7

If you don't care to validate the objects that get pushed into the likes property, you can set blackbox to true in your schema, like so:

likes: {
    type: [Object],
    optional: true,
    blackbox: true
}

This will allow you to put whatever you want into a "like" object.

If you do want to validate the "like" objects, then you'll need to create some additional schemas, like so:

var likeInfoSchema = new SimpleSchema({
    genre: {
        type: String
    },
    length: {
        type: String
    },
    language: {
        type: String
    }
});

var likeSchema = new SimpleSchema({
    movieId: {
        type: String
    },
    information: {
        type: likeInfoSchema
    }
});

Meteor.users.attachSchema(new SimpleSchema({
    // ...
    likes: {
        type: [likeSchema]
    }
}));
Sign up to request clarification or add additional context in comments.

2 Comments

This seems to have been the problem. It's weird that it just didn't refuse to enter anything like it behaves normally.
By default, collection2 will filter out any fields that are not specified in your schema before doing an insert or update without throwing an error. If you want an error to be thrown, then you must set the filter option to false when calling insert or update as in Meteor.users.update({_id: Meteor.userId()}, {$push: {likes: {movieId: movieId, information: informationObj}}}, {filter: false}).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.