Meteor.js&Collection2:更新在子模式中插入新对象的方法

Meteor.js & Collection2: Update Method that Inserts New Object in subschema

本文关键字:新对象 插入 对象 方法 子模式 amp js Collection2 更新 Meteor      更新时间:2023-09-26

上下文

我有一个收藏("场地")。我试图允许用户将新对象("房间"-定义为子模式)添加到场所对象的实例中。每个房间都应该有一个唯一的id(使用collection2模式中的autovalue)。

问题/我尝试了什么

我尝试了两种方法:

  1. Venues.insert-尝试创建一个新的整个Venue实例,而不是向roomPricing子架构添加新行
  2. Venues.update-使用"$set:params"抛出Exception while invoking method 'addRoom' MongoError: Cannot update 'roomPricing' and 'roomPricing.roomId' at the same time。如果我从集合中删除Id,应用程序将更新"现有"房间值,而不是创建房间的"新"实例

总之,我需要一个方法来更新"parent"对象(场所),同时在"room"子模式中创建新的"child"对象

系列

Schema.RoomPricing = new SimpleSchema({
    roomId: {
        type: String,
        autoValue: function(){
             return Random.id();
            },
        optional: true
    },
    roomName: {
        type: String,
        max:50,
        optional: true
      }
    }
// MAIN SCHEMA for the Venues colleciton. 
Schema.Venues = new SimpleSchema({
    venueName: {
        type: String,
        label: "Venue Name",
        max: 200,
        optional: false
},
   roomPricing: {
        type: Schema.RoomPricing,
        optional: true
  }
}

控制器&方法

  var currentVenueId = this.params._id
  var params = {
      roomPricing: {
          roomName: roomName,
          sitCapacity: sitCapacity,
        }
      }
  Meteor.call('addRoom', currentVenueId, params);
//Method
Meteor.methods({
  'addRoom': function (id, params) {
    Venues.insert({
        _id: id
        },{
        $set:params});
        }
    });

最后,我使用的是$push,而不是$set。

'addRoom': function (id, params) {
Venues.update({
     _id: id
   },{
     $push:params});
   }