From a2888250f4ab3615c1590ab3bbf90302963a7c57 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Fri, 22 Apr 2016 00:59:05 +0200 Subject: Models: Replace before.insert with autoValues The before.insert hooks have the problem, that they are executed in a different order if called from the client or from the server. If called from the client, the before.insert hook is called before validation of the schema, but if called from the server, the validation is called first and fails. --- models/cardComments.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'models/cardComments.js') diff --git a/models/cardComments.js b/models/cardComments.js index 224deb03..ce6edf3c 100644 --- a/models/cardComments.js +++ b/models/cardComments.js @@ -16,10 +16,22 @@ CardComments.attachSchema(new SimpleSchema({ createdAt: { type: Date, denyUpdate: false, + autoValue() { // eslint-disable-line consistent-return + if (this.isInsert) { + return new Date(); + } else { + this.unset(); + } + }, }, // XXX Should probably be called `authorId` userId: { type: String, + autoValue() { // eslint-disable-line consistent-return + if (this.isInsert && !this.isSet) { + return this.userId; + } + }, }, })); @@ -44,11 +56,6 @@ CardComments.helpers({ CardComments.hookOptions.after.update = { fetchPrevious: false }; -CardComments.before.insert((userId, doc) => { - doc.createdAt = new Date(); - doc.userId = userId; -}); - if (Meteor.isServer) { CardComments.after.insert((userId, doc) => { Activities.insert({ -- cgit v1.2.3-1-g7c22