Updating Sub-Collection - Schema Error

Hello guys,

I have a collection (Products) which has an array of elements that are themselves collections (Prices).
I created a method that inserts a Price and adds it to the corresponding Product. The insert part is working, but the method is unable to add the Price to the Product and gives me the following error:

Exception in callback of async function: Error: After filtering out keys not in the schema, your modifier is now empty
I20181114-13:41:19.660(-2)? at doValidate (packages/aldeed:collection2/collection2.js:423:11)
I20181114-13:41:19.660(-2)? at _0xb3b0x8.(anonymous function).Mongo.Collection.(anonymous function) [as update] (packages/aldeed:collection2/collection2.js:195:14)
I20181114-13:41:19.660(-2)? at imports/api/methods.js:67:22
I20181114-13:41:19.661(-2)? at wrappedCallbackForParsingMongoValidationErrors (packages/aldeed:collection2/collection2.js:530:15)
I20181114-13:41:19.661(-2)? at packages/mongo/collection.js:727:7
I20181114-13:41:19.661(-2)? at runWithEnvironment (packages/meteor.js:1356:24)
I20181114-13:41:19.661(-2)? at packages/meteor.js:1369:14
I20181114-13:41:19.661(-2)? at packages/mongo/mongo_driver.js:331:7
I20181114-13:41:19.662(-2)? at runWithEnvironment (packages/meteor.js:1356:24)

This is my code:

/collections.js

export const PriceRegisters = new Mongo.Collection('priceregisters');
export const Products = new Mongo.Collection('products');

const PriceRegisterSchema = new SimpleSchema({
    url: {
        type: String,
        label: "URL",
        optional: true
    },
    date: {
        type: Date,
        label: "Date",
        optional: true
    },
    price: {
        type: Number,
        label: "Price",
        optional: true
    },
    currency: {
        type: String,   
        label: "Currency",
        optional: true
    }
});

PriceRegisters.attachSchema(PriceRegisterSchema);

const ProductSchema = new SimpleSchema({
    name: {
        type: String,
        label: "Name",
        optional: true
    },
    createdAt: {
        type: Date,
        label: "Date",
        optional: true
    },
    priceRegisters: {
        type: Array,
        optional: true
    },
    'priceRegisters.$': {
        type: PriceRegisters,
        optional: true
    },
    users: {
        type: Array,
        optional: true
    },
    'users.$': {
        type: String,
        optional: true
    }
});

Products.attachSchema(ProductSchema);

/methods.js

Meteor.methods({

     'addProduct': function(name,userId){
        Products.insert({
            name: name,
            createdAt: Date.now(),
            priceRegisters: [],
            users: [userId]
        });
    },

    'addPrice': function(price,currency,url,productName){

        const newRegister = {
            url: url,
            date: Date.now(),
            price: price,
            currency: currency
        }

       let priceRegister = {};

        PriceRegisters.insert(newRegister,function(error,result){
            priceRegister = PriceRegisters.findOne({_id: result});
            Products.update({name:productName},{$push: {priceRegisters:priceRegister}});
        });

})

Any ideas what I am doing wrong?
There is a section about problems with sub-objects here (https://github.com/aldeed/meteor-collection2), but I couldn’t find the solution for this there or elsewhere.

Note: I am using simpl-schema and collection2

Thanks!

This looks like it should work, but before debugging it, why are you storing the whole priceRegister document in two place?

Why not just put the priceRegister IDs into the array, like you’re doing with Users?

As for the error, my guess is that it’s because the priceRegister Object getting inserted has an _id property, and the schema does not?

1 Like