Am still getting the hang of promises..
Here are the models of the db collections involved:
var itemSchema = new Schema({
label : String,
tag : { "type": Schema.ObjectId, "ref": "tag" }
});
var tagSchema = new Schema({
label : String,
});
And here's the series of Promises (map, map, map, join):
Right now, the Promise.join saves&completes before the 'items' map finishes running, and so the 'senders' map doesn't include the 'itemsArray' javascriptObject on save .. how can this be resolved?
var reqItems = req.body.items;
var itemsArray = [];
var items = Promise.map(reqItems,function(element){
var existingItem = Models.Item.findOneAsync({ "label": element });
existingItem.then(function (value) {
if ( (existingItem.fulfillmentValue != null) ) { // if this item exists
var itemObject = [
{ "item" : existingItem.fulfillmentValue._id },
{ "label" : existingItem.fulfillmentValue.label }
{ "tag" : existingItem.fulfillmentValue.tag }
];
itemsArray.push(itemObject);
} else { // first instance of this item .. create newItem
var existingTag = Models.Tag.findOneAsync({ "label": element });
existingTag.then(function (value) {
if ( (existingTag.fulfillmentValue != null) ) { // if this tag exists
var newItem = new Models.Item(
{
label : element,
tag : existingTag.fulfillmentValue._id,
}
);
newItem.save(function (err) { // save the newItem with existing tag
console.log(err);
var newSavedItem = Models.Item.findOneAsync({ "label": element });
newSavedItem.then(function (value) { // ensure existence of newItem
var itemObject = [
{ "item" : newSavedItem.fulfillmentValue._id },
{ "label" : newSavedItem.fulfillmentValue.label },
{ "tag" : newSavedItem.fulfillmentValue.tag }
];
itemsArray.push(itemObject); // push item to array
});
});
} else { // else this tag does not exist
var newTag = new Models.Tag(
{
label : element
}
);
newTag.save(function (err) {
console.log(err);
var newSavedTag = Models.Tag.findOneAsync({ "label": element });
newSavedTag.then(function (value) { // ensure existence of newTag
if ( (newSavedTag.fulfillmentValue != null) ) {
var newItem = new Models.Item(
{
label : element,
tag : newSavedTag.fulfillmentValue._id,
}
);
newItem.save(function (err) {
console.log(err);
var newSavedItem = Models.Item.findOneAsync({ "label": element });
newSavedItem.then(function (value) { // ensure existence of newItem
var itemObject = [
{ "item" : newSavedItem.fulfillmentValue._id },
{ "label" : newSavedItem.fulfillmentValue.label },
{ "tag" : newSavedItem.fulfillmentValue.tag }
];
itemsArray.push(itemObject); // push item to array
}); // newSavedItem.then
}); // newItem.save
} // if newSavedTag.isFulfilled
}); // newSavedTag.then
}); // newTag.save
} // else tag does not exist
}); // existingTag.then
} // first instance of this item .. create newItem
}); // existingItem.then
itemObject = null; // reset for map loop
}); // Promise.map itemsArray
var receivers = Promise.map(receiverArray,function(element){
return Promise.props({
username : element
});
});
var senders = Promise.map(senderArray,function(element){
return Promise.props({
username : element,
items : itemsArray
});
});
Promise.join(receivers, senders, function(receivers, senders){
store.receivers = receivers;
store.senders = senders;
var saveFunc = Promise.promisify(store.save, store);
return saveFunc();
}).then(function(saved) {
console.log(saved);
res.json(saved);
})...error handling...});