上下文
我有一个收藏(“ field ”)。我试图允许用户将新对象(“房间” - 定义为子模式)添加到 field 对象的实例中。每个房间都应该有一个唯一的 ID(在 collection2 架构中使用 autovalue)。
问题/我尝试过的
我尝试了两种方法:
- Venues.insert - 尝试创建一个新的整个 Venue 实例,而不是向 roomPricing 子架构添加新行。
- Venues.update - 使用 '$set:params"抛出
Exception while invoking method 'addRoom' MongoError: Cannot update 'roomPricing' and 'roomPricing.roomId' at the same time
。如果我从集合中删除 Id,应用程序将更新“现有”房间值,而不是创建房间的"new"实例。里>
总之,我需要一种方法来更新“父”对象( field ),同时在“房间”子模式中创建新的“子”对象。
收藏
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
}
}
Controller 和方法
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});
}
关于javascript - Meteor.js 和 Collection2 : Update Method that Inserts New Object in subschema,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34299160/