meteormeteor-autoform

Meteor Methods doesn't update database using Autoform


Using autoform, it seems the data is being passed from autoform as the Meteor method on my server does get the data, but then doing the database update inside of my method doesn't update my database...what am I missing?

Autoform code...

{{> quickForm collection="Rooms" type="method-update" 
      doc=this autosave=true id=makeUniqueID 
meteormethod="updateRoom"}}

Meteor method:

updateRoom: function (room) {
  console.log(room);
  Rooms.update({_id: room._id}, { $set: {
    checkIn: room.checkIn,
    checkOut: room.checkOut,
    tenantID: room.tenantID,
    available: room.available,
    needCleaning: room.needCleaning,
}});
},

My allow/deny rules:

Rooms.allow({
 insert() { return false; },
 update() { return false; },
 remove() { return false; }
});

Rooms.deny({
 insert() { return true; },
 update() { return true; },
 remove() { return true; }
});

Below is what I get from the console log in my from my meteor method. So I do get the changes (in this case change the tenantID and the false to available), but it doesn't update in the database. I'm missing a little detail somewhere but can't see it at this point.

enter image description here


Solution

  • The room variable you are passing to the method is nesting everything under the modifier and $set: keys.

    You could simply do:

    updateRoom: function (room) {
      Rooms.update({_id: room._id}, room.modifier);
    },
    

    but that's really insecure because you're passing the whole modifier to the method and a hacker could pass in anything they wanted to.

    Better:

    updateRoom(room) {
      check(room,Object);
      check(room._id,String);
      {checkIn, checkOut, tenantId, available, needCleaning } = room.modifier.$set;
      Rooms.update(room._id, { $set: {checkIn, checkOut, tenantId, available, needCleaning }}); 
    },