Meteor: $addToSet inserts empty object into array despite valid parameters

Created on 3 Feb 2016  路  1Comment  路  Source: meteor/meteor

I have a meteor method that makes the following call:

Groups.update("thesamegroupId", {
    $addToSet: { 
        contacts: { 
            name: 'somestring', 
            email: "anotherstring", 
            phone: "anotherstring", 
            _id: "anotherstring" 
        }
    }
});

And will insert an empty object into the contacts array. Using an identical query in the meteor mongo console however produces a different result.

db.groups.update("thesamegroupId", {
    $addToSet: { 
        contacts: { 
            name: 'somestring', 
            email: "anotherstring", 
            phone: "anotherstring", 
            _id: "anotherstring" 
        }
    }
});

Will correctly insert the described object onto the contacts array.

I've tried for two hours to google foo this, and have come up with nothing. Any help would be appreciated, do not hesitate to ask for more info if needed.

Most helpful comment

SOLVED: This was caused by my usage of the simple schema package, I did not declare the properties on the object in the contacts array, which caused Simple Schema to clean the objects into empty objects, because the name, email, phone, and _id, properties were not properly declared

The following code is necessary for $addToSet to function:

collectionName.arrayField.$.objectProperty: {
    type: <type>
}

For every property you tend to have on the object, otherwise it will be stripped.

>All comments

SOLVED: This was caused by my usage of the simple schema package, I did not declare the properties on the object in the contacts array, which caused Simple Schema to clean the objects into empty objects, because the name, email, phone, and _id, properties were not properly declared

The following code is necessary for $addToSet to function:

collectionName.arrayField.$.objectProperty: {
    type: <type>
}

For every property you tend to have on the object, otherwise it will be stripped.

Was this page helpful?
0 / 5 - 0 ratings