Validations running multiple times


#1

Hi I am using meteor-collection2 with mdg:validated-method. Below is my code:

Posts.schema = new SimpleSchema({
    title: {
        type: String
    },
    description: {
        type: String
    },
    posted: {
        type: Date,
        autoValue: function(): Object {
            console.log("setting date:" + this.isInsert + this.isUpsert);
            return new Date();
        }
    }
});

Posts.attachSchema(Posts.schema);

Here is my Validated method:

export const addPost = new ValidatedMethod({
    name: 'Posts.addPost',
    validate: Posts.schema.validator({ clean: true, filter: false }),
    run(post) {
        if (!this.userId)
            throw new Meteor.Error('403', 'You must be logged-in to reply');

        Posts.insert({
            title: post.title,
            description: post.description,
        });
    }
});

Issue is i am seeing 3 console logs on client with below information:

setting date:undefinedundefined
setting date:truefalse
setting date:truefalse

and 2 console logs on server

setting date:undefinedundefined
setting date:truefalse

I have following questions:

  1. Why i am seeing multiple console logs (3 on client and 2 on server)
  2. why this.isInsert and this.isUpsert are undefined for the first time

#2

Isn’t the context of this changing? Do a console.log(this) and see what you get there.


#3

you are right. this should change. It prints [object Object]. I am not sure how i can get the values out of this

I removed the validation in ValidatedMethod by setting

validate: null

So now validation is run automatically when insert happens. On server side i can see one console log

setting date:truefalse

But on client side i can still see 2 console logs:

setting date:truefalse
setting date:truefalse

Some questions:

  1. Is it right to set validate:null as collection2 module ensures that it will validate schema whenever any insert or update is done
  2. Why 2 console logs on client side