【问题标题】:Meteor Methods doesn't update database using AutoformMeteor Methods 不使用 Autoform 更新数据库
【发布时间】:2017-12-03 05:28:33
【问题描述】:

使用 autoform,似乎数据是从 autoform 传递的,因为我服务器上的 Meteor 方法确实获取了数据,但是在我的方法内部进行数据库更新不会更新我的数据库...我错过了什么?

自动生成代码...

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

流星法:

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,
}});
},

我的允许/拒绝规则:

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

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

以下是我从我的流星方法中的控制台登录得到的。所以我确实得到了更改(在这种情况下,将tenantID 和 false 更改为可用),但它不会在数据库中更新。我在某处遗漏了一些细节,但此时看不到。

【问题讨论】:

    标签: meteor meteor-autoform


    【解决方案1】:

    您传递给方法的room 变量将所有内容嵌套在modifier$set: 键下。

    你可以这样做:

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

    但这确实不安全,因为您将整个修饰符传递给该方法,而黑客可以传递他们想要传递的任何内容。

    更好:

    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 }}); 
    },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-28
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      • 2016-01-09
      • 2016-01-24
      • 2015-09-20
      相关资源
      最近更新 更多