dens14345
dens14345

Reputation: 302

Removing Embedded object in array in meteor error

Ive researched and seen questions in how to remove embedded documents in mongodb/meteor here in stackoverflow and on meteor forums. But I can't figure out why $pull wont work for me.

Been stuck in this problem for days. I have a collection that looks like this.

{
  "_id": "N6QAJQpq4p5aYbpev",
  "name": "Test Project",
  "description": "description here",
  "organization": "xPtaobSBQwxCKAJLN",
  "members": [
    {
      "id": "3b7sbW3x487PzzJ6h",
      "role": "manager",
      "dateAdded": "2018-01-24T17:49:50.734Z"
    },
    {
      "id": "n72PveQmdjcsvv5t5",
      "role": "manager",
      "dateAdded": "2018-01-24T17:49:53.545Z"
    },
    {
      "id": "dSqzezqQjrzLeSTEw",
      "role": "manager",
      "dateAdded": "2018-01-24T17:50:11.177Z"
    }
  ]
}`

my method in updating the record:

removeUserFromProj: (projId, memberId) =>{
  Projects.update({ _id: projId }, {
     $pull: { "members": { id: memberId } }
     // $pull: { "members.id":  memberId  }
  },
     function (error, success) {
        if (error) {
           console.log('error: ', error);
        }
        if (success) {
           console.log('updating user role: ' + success);
        }
     });

}

the callback function is giving me a success

enter image description here

but the collection is not updating.

If i use this syntax // $pull: { "members.id": memberId } its giving me this error saying:

simulating the effect of invoking 'removeUserFromProj' Error: After filtering out keys not in the schema, your modifier is now empty

I also tried using $pop instead of $pull. It is removing the last record in my collection.

enter image description here

I am using meteor SimpleSchema.

Upvotes: 0

Views: 96

Answers (1)

dens14345
dens14345

Reputation: 302

Found my bug. It seems like the autoValue property in my schema is preventing me to update my

OrganizationSchema = new SimpleSchema({
       name: {
          type: String,
          label: "Organization Name"
       },
       createdAt: {
          type: Date,
          autoValue: function(){
             return new Date()
          }  
       },
       createdBy:{
          type: String
       },
       members: {
          type: [Member],
          optional: true
       }
    });

changing the autoValue Property to

createdAt: {
   type: Date,
   autoValue: function(){
             return new Date()
   }  
}

defaultProperty Solved my issue:

createdAt: {
      type: Date,
      defaultValue:  new Date()
   },

Upvotes: 0

Related Questions