How to update collection in "Collection-Hook" but get error with "SimpleSchema" validation?


#1

I want to update Collection on Collection-Hook like this

// Collection
Collection.Customer = new Mongo.Collection("customer");

// Schema
Schema.Customer = new SimpleSchema({
    name: {
        type: String,
        label: "Name",
        max: 200
    },
    gender: {
        type: String,
        label: "Gender"
    },
    dob: {
        type: String,
        label: "Date of Birth"
    },
    telephone: {
        type: String,
        label: "Telephone",
        optional: true
    }
});

// Attach schema
Collection.Customer.attachSchema(Schema.Customer);

And then I want to update Customer Name on Hoook but get error with validate

Collection.Other.after.update(function (userId, doc, fieldNames, modifier, options) {
    modifier.$set = modifier.$set || {};

    Collection.Customer.update({}, {$set: {name: 'Rabbit'}}, {multi: true})
});

How to solve this?


#2

Your second bit of code seems inconsistent. I’m not familiar with the specifics of the collection-hooks package, but first doing modifier.$set = … and then not using that smells like an issue with the code. You should probably just leave that line off?
EDIT: Those parameters you get from the hook should usually be read-only, though I could be wrong in trying to interpret the collection-hooks API without looking at the docs (c:

Also, please post the complete error message you’re getting next time. Even if it’s not a particularly enlightening one, like is often the case with the simple-schema error messages :wink: