Hi guys,
I am trying to use collection2 in combination with collection-hooks to keep 2 collections in sync.
Its an icecreamshop test-project. There is a “Scoop” collection and an Order collection. Basically a customer can oder multiple scoops. The scoop-instance is then embedded within the Order-Collection.
The problem is that when I change the name of a scoop in the “Scoop”-Collection,
the Scoops.after.update
triggers, but only updates the first element in the order collection.
How do you guys handle this in meteor? (I think it is a mongodb problem? http://stackoverflow.com/questions/4669178/how-to-update-multiple-array-elements-in-mongodb)
This is the code I have:
AutoformScoopOrders = new Mongo.Collection('autoformscooporders');
// SCHEMA
autoformScoopOrdersSchema = new SimpleSchema({
// Customer-Name
customerName: {
type: String
},
// scoopItems
scoopItems: {
type: Array, // 1 invoice can have m invoiceItems
optional: true,
},
'scoopItems.$': {
type: Object
},
'scoopItems.$.amount': {
type: Number,
},
'scoopItems.$.scoopId': {
type: String,
autoform: { // Foreign-Key
options: function () {
return Scoops.find().map(function (c) {
return {label: c.name + '( ' + c.price + '€ )', value: c._id};
});
}
},
},
'scoopItems.$.scoopInstance': {
type: Object,
blackbox: true, // skip validation for all included fiels
optional: true,
autoValue: function () {
var theScoopInstanceId = this.siblingField('scoopId');
var theScoopInstance = theScoopInstanceId.isSet && Scoops.findOne(theScoopInstanceId.value);
return _.omit(theScoopInstance, '_id');
},
autoform: {
omit: true,
},
},
});
AutoformScoopOrders.attachSchema(autoformScoopOrdersSchema); // Anhängen an das Schema
// HOOKS
Scoops.after.update(function (userId, doc, fieldNames, modifier, options) {
if (Meteor.isServer) {
if (Scoops._transform(doc).hasDataChanged(this.previous)) {
// ERROR: ONLY THE FIRST ITEM IS UPDATED
// AM I DOING SOMETHING WRONG?
nrOfUpdates = AutoformScoopOrders.update(
{'scoopItems.scoopId': doc._id},
{$set: { 'scoopItems.$.scoopInstance': doc }},
{multi: true}
);
}
}
});
Any suggestions?