Hi group,
My challenge should be common but I can find it described in the forum, so I must using the wrong approach.
I have related objects. Let say Harvest and Harvest Details. They really exist jointly, like Orders and OrderItems. And I'd rather ´save´ them at once, so a subdocument relation sounds appropriate
The (simplified) schemas:
var HarvestDetailsSchema = new Schema({
amount: Number,
name: String
});
/**
* Harvest Event Schema
*/
var HarvestSchema = new Schema({
harvesttime: {type: Date},
user: { type: Schema.ObjectId,ref: 'User'},
mac: String,
details: [HarvestDetailsSchema]
});
The server side save function.
harvest.save(function (err) {
if (err) {... });
var i = harvest.details.length;
while (i--) {
console.log(i);
detail = harvest.details[i];
Tree.findOne({ mac: detail.mac }, '_id',findTreeCB);
}
});
In the while countdown loop I try to take some action (create or update other objects), based on the current <detail> object, based on i. It does not have to be count down, the actions are independent.
The problem is by the time findTreeCB is called back, the while loop has completed and the <detail> object seen by the findTreeCB callback is the last value. I understand the closure problem, but need a solution.
I think it should work if I could simply let the callback know about the current instance of <detail>.
I've tried to force some wrapper functions around the findOne and also the callback function, with no success. I do not discard my code had problems but I suspect the issue is in the Mongoose library.
Is there a way I can pass a variable (here <detail>) to the calback function that is not the err or findOne result?
On the other had, I'm open to consider a different way of doing things. I'm not to familiar with this async model and I might be thinking too much in a procedural way. There are pre and post middleware that I see I could use. But I do not yet see how I can work around the closure and the callback.
Thanks for any guidance.