2

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

Raggamuffin
  • 699
  • 1
  • 6
  • 19

2 Answers2

1

have you tried

$setOnInsert: {
    imageId: this._id,
    imageName: this.name,
    timesViewed: 0
},
Termininja
  • 6,620
  • 12
  • 48
  • 49
MrE
  • 19,584
  • 12
  • 87
  • 105
  • 1
    just saw your thread prequel, and if that didn't work, it sounds like a bug. `timesViewed` should only be set on insert, after that just increment should take action. Have you tried the query in mongo directly to see the behavior: you need to use `update` with `{upsert: true}` – MrE Apr 13 '16 at 00:58
0

Ok i played around a little based on your suggestions and the previous thread and this solution works without errors and expected results:

Schema:

Data = {};
Data.Viewed = new Mongo.Collection("dataViewed", {});
Data.Viewed.Schema = new SimpleSchema({
    imageId: {
        type: String
    },
    userId: {
        type: String,
        autoValue: function() {
            return this.userId;
        }
    },
    imageName: {
        type: String
    },
    timesViewed: {
        type: Number
    },
    createdAt: {
        type: Date,
        autoValue: function() {
            return new Date();
        }
    }
});
Data.Viewed.attachSchema(Data.Viewed.Schema);

Method:

Meteor.methods({
    dataViewed(obj) {
        Data.Viewed.upsert({
            imageId: obj._id,
            userId: this.userId
        }, {
            $setOnInsert: {
                imageId: obj._id,
                userId: this.userId,
                imageName: obj.term,
                timesViewed: 0
            },
            $inc: {
                timesViewed: 1
            }
        });
    }
});

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!

Raggamuffin
  • 699
  • 1
  • 6
  • 19
  • This was very helpful, but for me just now, I had to remove the timesViewed: 0 from the $setOnInsert or I got a "cannot update timesViewed and timesViewed at the same time" error from mongo. – RealHandy Jul 29 '18 at 00:32