I have an image gallery with a forward and backward-button. on a click on either of the buttons i want to upsert an entry in the local database with the times the image has been viewed (so i can later see which image has been viewed the most).
This works perfectly without the Schema:
'click .btn-forward, click .btn-backward' (event, template) {
Local.Viewed.upsert({
imageId: this._id
}, {
$setOnInsert: {
imageId: this._id,
imageName: this.name
},
$inc: {
timesViewed: 1
}
});
}
});
The Schema:
Local.Viewed.Schema = new SimpleSchema({
imageId: {
type: String
},
imageName: {
type: String
},
timesViewed: {
type: Number,
defaultValue: 0
},
createdAt: {
type: Date,
autoValue: function() {
return new Date();
}
}
});
Problem:
When i use this Schema i get an error:
update failed: Error: Times viewed is required at getErrorObject
The Schema seems to be demanding that 'timesViewed' is set. I tried using 'defaultValue: 0' in the Schema but that doesn't insert a default Value of 0.
Question: How can i make the Schema compatible with this query?
Thanks for your help!
Muff
Ok i played around a little based on your suggestions and the previous thread and this solution works without errors and expected results:
Schema:
Method:
I think the problem was that i defined an defaultValue/ autoValue in the Schema for 'timesViewed'. Further every property that is mentioned in the schema must be mentioned in the $set or $setOninsert command.
Thanks for your help!
have you tried