Meteor Mongo - upsert and $inc with aldeed simple schema - 'update failed'

这一生的挚爱 提交于 2019-12-12 18:36:06

问题


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


回答1:


have you tried

$setOnInsert: {
    imageId: this._id,
    imageName: this.name,
    timesViewed: 0
},



回答2:


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!



来源:https://stackoverflow.com/questions/36575144/meteor-mongo-upsert-and-inc-with-aldeed-simple-schema-update-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!