Find all in-between connections from a node to other in jsPlumb - javascript

In jsPlumb, I am trying to get the full path between 2 nodes as an array with a single query even if there is multiple nodes in between source and destination. I am currently doing it with a BFS algorithm because I couldn't find anything like that in the documentation the code is
getPath: function (sourceId, targetId) {
var me = this;
var addNode = function addNode(graph, node) {
graph.set(node, {
in: new Set(),
out: new Set()
});
};
var connectNodes = function connectNodes(graph, sourceId, targetId) {
graph.get(sourceId).out.add(targetId);
graph.get(targetId).in.add(sourceId);
};
var buildGraphFromEdges = function buildGraphFromEdges(edges) {
return edges.reduce(function (graph, {
sourceId,
targetId
}) {
if (!graph.has(sourceId)) {
addNode(graph, sourceId);
}
if (!graph.has(targetId)) {
addNode(graph, targetId);
}
connectNodes(graph, sourceId, targetId);
return graph;
}, new Map());
};
var buildPath = function buildPath(targetId, path) {
var result = [];
while (path.has(targetId)) {
var sourceId = path.get(targetId);
result.push({
sourceId,
targetId
});
targetId = sourceId;
}
return result.reverse();
};
var findPath = function findPath(sourceId, targetId, graph) {
if (!graph.has(sourceId)) {
throw new Error('Unknown sourceId.');
}
if (!graph.has(targetId)) {
throw new Error('Unknown targetId.');
}
var queue = [sourceId];
var visited = new Set();
var path = new Map();
while (queue.length > 0) {
var start = queue.shift();
if (start === targetId) {
return buildPath(start, path);
}
for (var next of graph.get(start).out) {
if (visited.has(next)) {
continue;
}
if (!queue.includes(next)) {
path.set(next, start);
queue.push(next);
}
}
visited.add(start);
}
return null;
};
var graph = buildGraphFromEdges(me.jsPlumbContainer.getAllConnections());
var resultPath = findPath(sourceId, targetId, graph);
return resultPath;}
Is there a much better way to implement this ?
example array for my configuration is:
var connections =[
{
"sourceId": "l1",
"targetId": "l2"
},
{
"sourceId": "l2",
"targetId": "l4"
},
{
"sourceId": "l2",
"targetId": "l3"
},
{
"sourceId": "l4",
"targetId": "l5"
}, ]

Related

How to remove double quotes from an array of strings

Say I have the array below
var someStuff = [
"start", { one: "one"},
"setSomething", "foobar",
"someBool", true
];
How can I transform it to be as below
var someStuff = [
MainFunc.start({one: "one"}),
MainFunc.setClientId('foobar'),
MainFunc.someBool(true)
];
Assuming you want an array containing results from a number of function calls, this is how you can get that. You need to iterate over the array, get each function from the MainFunc object and then call that each time.
I included handling for a non-existing function being called, which happened to be in your example (perhaps not intentional).
var MainFunc = {
start : function(x) { return "start called with " + x.toString(); },
setClientId : function(x) { return "setClientId called with " + x.toString(); },
someBool : function(x) { return "someBool called with " + x.toString(); }
}
var someStuff = [
"start", { one: "one" },
"setSomething", "foobar",
"setClientId", "foobar",
"someBool", true
];
var result = Process(someStuff);
function Process(arr) {
var result = [];
for (var i = 0; i < arr.length; i += 2) {
let fnToCall = MainFunc[arr[i]];
let fnResult = fnToCall ? fnToCall(arr[i + 1]) : "(unknown: '" + arr[i] + "')";
result.push(fnResult);
}
return result;
}
console.log(result);
Try it like this (You need to use arrow functions because otherwise the function would get called straigh away):
var someStuff = [
"start", { one: "one"},
"setSomething", "foobar",
"someBool", true
];
let output = []
for(let i=0, j=0; i<someStuff.length; i+=2, j++) {
let functionName = someStuff[i];
let functionArgs = someStuff[i+1];
output[j] = () => MainFunc[functionName](functionArgs);
}
console.log(output);
I think you might want to do this
function mainFunc() {
this.start = function(obj) { console.log("start",obj) };
this.setSomething = function(obj) { console.log("set",obj) };
this.someBool = function(obj) { console.log("bool",obj) };
}
var someStuff = {
"start": { "one":"one"},
"setSomething": "foobar",
"someBool": true
};
var mf = new mainFunc();
for (var a in someStuff) {
mf[a](someStuff[a])
}
For better readability, define an array of objects with action and its parameter.
const mainFunc = {
start: (param) => console.log('start', param),
setSomething: (param) => console.log('SetSomething', param),
someBool: (param) => console.log('someBool', param)
}
var someStuff = [
{ action: "start", param: { one: "one"} },
{ action: "setSomething", param: "foobar" },
{ action: "someBool", param: true }
];
someStuff.forEach(stuff => {
if (typeof mainFunc[stuff.action] === 'function') {
mainFunc[stuff.action](stuff.param);
}
});

Issues printing my groups tree in nodeJS app

I'm trying to print all created groups and they're children so it'll look like that:
[ [ 'Father1', 'Child1', 'Child2', 'Child3' ],
[ 'Father1', 'Child1', 'Child4' ],
[ 'Father1', 'Child1', 'Child5' ] ]
The problems I encountered are varied. from:
var keys = name.keys(o); ^ TypeError: name.keys is not a function to total stack overflow, iv'e debugged the printPath function and it's doing it's job separately but not with my final tree structure.
My tree and print function looks like that:
groups.js:
class groups {
constructor() {
this.root = new Group('root');
}
printPath(name){
this.root.getPath(name)
}
group.js:
class Group {
constructor(name, parent) {
this.name = name;
this.parent = parent || null;
this.children = [];
this.users = new users || null;
}
getPath(name) {
function iter(o, p) {
var keys = name.keys(o);
if (keys.length) {
return keys.forEach(function (k) {
iter(o[k], p.concat(k));
});
}
result.push(p);
}
var result = [];
iter(name, []);
return result;
}
Edit:
For creating a group i'm using a menu handler function:
function createGroup(callback) {
rl.question('Add name for father group: \n', (parent) => {
let parentGroup = programdata.groups.findGroupByName(parent);
if (!parentGroup) {
parentGroup = programdata.groups.root;
}
rl.question('name of new group\n', (groupName) => {
parentGroup.setChildren(new Group(groupName, parentGroup));
console.log(parentGroup);
callback();
});
})
}
findGroupByNameis a nice recursion i made that finds nested groups (feel free to use!) sitting in class groups.
findGroupByName(name) {
if (!name) return null;
return this._findGroupByNameInternal(this.root, name);
}
_findGroupByNameInternal(group, name) {
if (!group) return null;
if (group.name === name) return group;
for (const g of group.children) {
const result = this._findGroupByNameInternal(g, name);
if (!result) continue;
return result;
}
}
And setChildren function placed in class Group:
setChildren(child) {
this.children.push(child);
}
EDIT:
Thank you for the answer, could you please help me realize your method in my menu handler? iv'e tried this: and it giving me nothing.
function createGroup(callback) {
rl.question('Add name for father group: \n', (parent) => {
let parentGroup = programdata.groups.findGroupByName(parent);
let treePath = Group.root.printPath();
if (!parentGroup) {
parentGroup = programdata.groups.root;
}
rl.question('name of new group\n', (groupName) => {
parentGroup.addChild(new Group(groupName, parentGroup));
console.log(treePath);
callback();
});
})
}
The root cause you got the error TypeError: name.keys is not a function is that a string is passed into getPath(name) as argument name, you know the JS string object doesn't have a function property keys.
I refactor your code and fix some error, here is the testable version. Pls put them into the same folder and run test.js.
group.js
class Group {
constructor(name, parent) {
this.name = name;
this.parent = parent || null; // Point to this group's father
this.children = []; // Children of this group, can be sub-group or string
if (!!parent) { // Link to the father
parent.addChild(this);
}
// this.users = new users || null; // Useless, remove it.
}
addChild(...args) {
for(let o in args) {
this.children.push(args[o]);
}
}
/**
* Recursion to build the tree
* #param group
* #returns {*}
*/
iter(group) {
let children = group.children;
if (Array.isArray(children)) { // If the child is a group
if (children.length > 0) {
let result = [];
result.push(group.name);
for (let child of children) {
result.push(group.iter(child));
}
return result;
}
else {
return [];
}
}
else { // If the group is a string
return group;
}
}
getPath() {
return this.iter(this);
}
}
module.exports = Group;
groups.js
let Group = require('./group');
class Groups {
constructor() {
this.root = new Group('root');
}
printPath() {
return this.root.getPath();
}
}
module.exports = Groups;
test.js
let Group = require('./group');
let Groups = require('./groups');
// Root
let rootGroups = new Groups();
// Group 1
let group1 = new Group('Father1', rootGroups.root);
group1.addChild('Child1', 'Child2', 'Child3');
// Group 2
let group2 = new Group('Father1', rootGroups.root);
group2.addChild('Child1', 'Child4');
// Group 3
let group3 = new Group('Father1', rootGroups.root);
group3.addChild('Child1', 'Child5');
let treePath = rootGroups.printPath();
console.log(treePath);
The output is:
[ 'root',
[ 'Father1', 'Child1', 'Child2', 'Child3' ],
[ 'Father1', 'Child1', 'Child4' ],
[ 'Father1', 'Child1', 'Child5' ] ]
Process finished with exit code 0
Enjoy it :)
Ok, found a solution.
Treeshow(){
var node = this.root;
var depth = '-'
recurse( node );
function recurse( node) {
depth +='-'
console.log(depth+node.name);
for (var child in node.children ) {
recurse(node.children[child]);
}
depth = depth.slice(0, -1);
}
}
that will show my tree just like that:
--root
---FooFather
----BarSemiFather
-----FooChild
------BarBaby

How To Run Controller Functionality Again Using Different Parameters From View ng-model

I have a controller that works fine on initial load. It calls user [0] data and everything processes fine. When I change dropdown, I want to call the same function, but I cannot get the entire controller to reload. It starts from the function call and leaves undefined in places where it pulls correct information (linkToken, etc) on initial load. Is there a way I can get it to reload all data from the controller instead of just from the function?
After calling the testchange() from the view to pass in the new data, I get :
TypeError: Cannot read property 'locations' of undefined
at h.$scope.updateLocations (refillController.js:261)
at refillController.js:73
But, when I call the original getRefills() that is ran on the initial page load I get the same error. How can I define these so the load after the onchange(0)?
angular.module('FinalApp.Controllers').controller('refillController', function ($rootScope, $scope, $location, $modal, userService, dependentsService, accountService, sharedCollections, configurationService, refillsService) {
$scope.user = userService.GetUserInformation();
$scope.userInfoArr = [];
//$scope.tests.push({'Name':$scope.user.FirstName, 'LinkToken':$scope.user.LinkToken});
$scope.userInfoArr = $scope.user.Dependants.map(function(item){return {'Name':item.FirstName, 'LinkToken':item.LinkToken}});
$scope.userInfoArr.splice(0, 0, {'Name': $scope.user.FirstName, 'LinkToken': $scope.user.LinkToken});
console.log($scope.userInfoArr);
$scope.finUserInfoArr = $scope.userInfoArr[0].LinkToken;
$scope.billingInfo = null;
$rootScope.showNavbar = true;
$scope.selectedMethod = null;
$scope.location = null;
$scope.payment = null;
$scope.refills = [];
$scope.deliverTypes = [];
$scope.locations = [];
$scope.payments = [];
$scope.allSelected = false;
$scope.loadingBillingInfo = false;
$scope.isMailOrder = false;
//Detect Mobile Switch Refill List To Grid
if(window.innerWidth <= 800) {
$scope.view = "Grid";
} else {
$scope.view = "List";
}
$scope.changeViewToList = function () {
$scope.view = "List";
};
$scope.changeViewToGrid = function () {
$scope.view = "Grid";
};
$scope.testchange = function(selectedTest) {
$scope.getRefills(selectedTest);
console.log(selectedTest);
};
$scope.getRefills = function (linkToken) {
$scope.allSelected = false;
$scope.loading = true;
refillsService.GetRefills(
linkToken,
$scope.selectedMethod,
$scope.location,
$scope.payment
).then(function (data) {
$scope.refills = [];
_.each(data.Prescriptions, function (item) {
fillRefills(item);
});
fillDeliverTypes(data.DeliveryTypes);
if (!$scope.selectedMethod)
$scope.selectedMethod = data.DeliveryTypeId;
if (!$scope.location)
$scope.location = data.PickupLocationId;
if (!$scope.payment)
$scope.payment = data.PaymentTypeId;
$scope.loading = false;
$scope.updateLocations();
})["catch"](function (error) {
console.log(error);
$scope.loading = false;
alertify.alert(configurationService.ErrorMessage("getting your refills", error.Message));
});
};
var fillRefills = function (item) {
//TODO-CallDoc temp fix
if (item.RefillClass == "CALL_DOCTOR") {
item.NextRefillDate = '1900-01-01T00:00:00'
}
var parsedDate = checkDate(moment(item.NextRefillDate).format('L'));
var lastrefill = checkDate(moment(item.LastDispenseDate).format('L'));
var expireDate = checkDate(moment(item.ExpirationDate).format('L'));
var status = (item.RefillStatus.indexOf("After") == -1) ? item.RefillStatus : "Refill " + item.RefillStatus;
$scope.refills.push({
selected: false,
rx: item.ScriptNo,
name: item.DrugName,
dose: item.UnitsPerDose,
dir: item.Instructions,
nextfill: parsedDate,
lastfill: lastrefill,
refillsLeft: item.NumRefillsLeft,
status: status,
msg: item.RefillMessage,
canSelect: item.IsRefillable,
refillClass: item.RefillClass,
lastDispenseQty: item.LastDispenseQty,
DaysSupply: item.DaysSupply,
expireDate: expireDate,
copayAmt: item.CopayAmt,
drFirstName: item.DoctorFirstName,
drLastName: item.DoctorLastName,
writtenQty: item.WrittenQty
});
};
var checkDate = function (date) {
if (date == "01/01/1900") return "N/A";
if (date == "Invalid Date") return "";
return date;
};
var fillDeliverTypes = function (deliverTypes) {
$scope.deliverTypes = [];
_.each(deliverTypes, function (item) {
$scope.deliverTypes.push({
id: item.DeliveryTypeId,
name: item.DeliveryTypeName,
locations: item.PickupLocations,
payments: item.PaymentTypes
});
});
};
var getBillingInfo = function () {
$scope.loadingBillingInfo = true;
accountService.GetCreditCardInfo().then(function (data) {
$scope.billingInfo = data;
$scope.loadingBillingInfo = false;
})["catch"](function (error) {
$scope.loadingBillingInfo = false;
alertify.alert(configurationService.ErrorMessage("getting account information", error.Message));
});
};
var getAccountInfo = function () {
accountService.GetAccountInfo().then(function (data) {
if (data.StatusCode == "SUCCESS") {
$scope.user = data;
userService.SaveUserInformation(data);
if ($scope.user.IsLinked) {
$rootScope.enableMyRefills = true;
$rootScope.enableMyReports = true;
window.location.hash = "#/refills";
} else {
$rootScope.enableMyRefills = false;
$rootScope.enableMyReports = true;
}
} else {
alertify.alert(configurationService.ErrorMessage("getting account information", data.StatusMessage));
}
})["catch"](function (error) {
alertify.alert(configurationService.ErrorMessage("getting account information", error.Message));
});
};
var openModal = function (viewUrl, controllerUrl, size, payload) {
var modalInstance = $modal.open({
templateUrl: viewUrl,
controller: controllerUrl,
size: size,
resolve: {
data: function () {
return payload;
}
}
});
return modalInstance;
};
$scope.toggleRxSelection = function(rx) {
if (rx.canSelect) {
rx.selected = !rx.selected;
$scope.evaluateAllSelected();
}
};
$scope.selectAll = function (data) {
// $scope.allSelected = allSelected;
_.each($scope.refills, function (x) {
if (x.canSelect) x.selected = data;
});
};
$scope.evaluateAllSelected = function () {
var count = _.countBy(_.where($scope.refills, {canSelect:true}), function(refill) {
return refill.selected ? 'selected' : 'not';
});
$scope.allSelected = (count.not === undefined);
};
$scope.openEditCreditCardInfo = function () {
var payload = ($scope.billingInfo != null && $scope.billingInfo != undefined)
&& $scope.billingInfo.CardNumber != "" ? $scope.billingInfo : {};
if (payload != {}) {
payload.ExpMonth = {id: parseInt(payload.ExpMonth)};
payload.ExpYear = {id: parseInt(payload.ExpYear)};
}
openModal('app/views/editAccount/billingInformation.html', "billingInformationController", "xlg", payload).result.then(function () {
getAccountInfo();
getBillingInfo();
}, function () {
getBillingInfo();
});
};
$scope.openConfirmOrder = function () {
var refillsSelected = _.where($scope.refills, {selected: true});
var location = _.findWhere($scope.locations, {PickupLocationId: $scope.location});
var payment = _.findWhere($scope.payments, {PaymentTypeId: $scope.payment});
var deliver = _.findWhere($scope.deliverTypes, {id: $scope.selectedMethod});
if (refillsSelected.length == 0) {
alertify.error("You need to select at least one refill");
return;
}
if (deliver.id == 10001 && !$scope.user.IsCreditCardOnFile) {
alertify.error("Need credit card on file for mail order");
return;
}
sharedCollections.setRefills(refillsSelected);
sharedCollections.setLocation(location);
sharedCollections.setPayment(payment);
sharedCollections.setDeliver(deliver);
openModal('app/views/refills/confirmOrder.html', "confirmOrderController", "xlg").result.then(function () {
$scope.billingInfo = accountService.GetCreditCardInfo();
$scope.getRefills();
}, function () {
//$scope.billingInfo = accountService.GetCreditCardInfo();
//getRefills();
});
};
$scope.showRefill = function (rx) {
var data = {rx: rx, refills: $scope.refills};
openModal('app/views/refills/showRefills.html', "refillsCarrousel", "xlg", data).result.then(function () {
$scope.evaluateAllSelected();
}, function () {
$scope.evaluateAllSelected();
});
};
$scope.updateLocations = function () {
$scope.locations = _.findWhere($scope.deliverTypes, {id: $scope.selectedMethod}).locations;
$scope.payments = _.findWhere($scope.deliverTypes, {id: $scope.selectedMethod}).payments;
setLocationAndPayment();
};
var setLocationAndPayment = function () {
if ($scope.locations.length == 1) {
$scope.location = $scope.locations[0].PickupLocationId;
}
if ($scope.payments.length == 1) {
$scope.payment = $scope.payments[0].PaymentTypeId;
}
//check for mail order
($scope.selectedMethod == 10001 && !$scope.payment) ? $scope.isMailOrder = true : $scope.isMailOrder = false;
};
$scope.getRefills($scope.finUserInfoArr);
getBillingInfo();
});
Check if your refillsService returns correct data, it could be that $scope.refills remains empty array.

SAPUI5 only update specific binding

In SAPUI5 I have a Model ("sModel") filled with metadata.
In this model I have a property "/aSelectedNumbers".
I also have a panel, of which I want to change the visibility depending on the content of the "/aSelectedNumbers" property.
update
first controller:
var oModelMeta = cv.model.recycleModel("oModelZAPRegistratieMeta", that);
//the cv.model.recycleModel function sets the model to the component
//if that hasn't been done so already, and returns that model.
//All of my views are added to a sap.m.App, which is returned in the
//first view of this component.
var aSelectedRegistratieType = [];
var aSelectedDagdelen = ["O", "M"];
oModelMeta.setProperty("/aSelectedRegistratieType", aSelectedRegistratieType);
oModelMeta.setProperty("/aSelectedDagdelen", aSelectedDagdelen);
First Panel (Which has checkboxes controlling the array in question):
sap.ui.jsfragment("fragments.data.ZAPRegistratie.Filters.RegistratieTypeFilter", {
createContent: function(oInitData) {
var oController = oInitData.oController;
var fnCallback = oInitData.fnCallback;
var oModel = cv.model.recycleModel("oModelZAPRegistratieMeta", oController);
var oPanel = new sap.m.Panel( {
content: new sap.m.Label( {
text: "Registratietype",
width: "120px"
})
});
function addCheckBox(sName, sId) {
var oCheckBox = new sap.m.CheckBox( {
text: sName,
selected: {
path: "oModelZAPRegistratieMeta>/aSelectedRegistratieType",
formatter: function(oFC) {
if (!oFC) { return false; }
console.log(oFC);
return oFC.indexOf(sId) !== -1;
}
},
select: function(oEvent) {
var aSelectedRegistratieType = oModel.getProperty("/aSelectedRegistratieType");
var iIndex = aSelectedRegistratieType.indexOf(sId);
if (oEvent.getParameters().selected) {
if (iIndex === -1) {
aSelectedRegistratieType.push(sId);
oModel.setProperty("/aSelectedRegistratieType", aSelectedRegistratieType);
}
} else {
if (iIndex !== -1) {
aSelectedRegistratieType.splice(iIndex, 1);
oModel.setProperty("/aSelectedRegistratieType", aSelectedRegistratieType);
}
}
// arrays update niet live aan properties
oModel.updateBindings(true); //******** <<===== SEE HERE
if (fnCallback) {
fnCallback(oController);
}
},
width: "120px",
enabled: {
path: "oModelZAPRegistratieMeta>/bChanged",
formatter: function(oFC) {
return oFC !== true;
}
}
});
oPanel.addContent(oCheckBox);
}
addCheckBox("Presentielijst (dag)", "1");
addCheckBox("Presentielijst (dagdelen)", "2");
addCheckBox("Uren (dagdelen)", "3");
addCheckBox("Tijd (dagdelen)", "4");
return oPanel;
}
});
Here is the panel of which the visibility is referred to in the question. Note that it DOES work after oModel.updateBindings(true) (see comment in code above), but otherwise it does not update accordingly.
sap.ui.jsfragment("fragments.data.ZAPRegistratie.Filters.DagdeelFilter", {
createContent: function(oInitData) {
var oController = oInitData.oController;
var fnCallback = oInitData.fnCallback;
var oModel = cv.model.recycleModel("oModelZAPRegistratieMeta", oController);
var oPanel = new sap.m.Panel( {
content: new sap.m.Label( {
text: "Dagdeel",
width: "120px"
}),
visible: {
path: "oModelZAPRegistratieMeta>/aSelectedRegistratieType",
formatter: function(oFC) {
console.log("visibility");
console.log(oFC);
if (!oFC) { return true; }
if (oFC.length === 0) { return true; }
return oFC.indexOf("2") !== -1;
}
}
});
console.log(oPanel);
function addCheckBox(sName, sId) {
var oCheckBox = new sap.m.CheckBox( {
text: sName,
selected: {
path: "oModelZAPRegistratieMeta>/aSelectedDagdelen",
formatter: function(oFC) {
if (!oFC) { return false; }
console.log(oFC);
return oFC.indexOf(sId) !== -1;
}
},
select: function(oEvent) {
var aSelectedDagdelen = oModel.getProperty("/aSelectedDagdelen");
var iIndex = aSelectedDagdelen.indexOf(sId);
if (oEvent.getParameters().selected) {
if (iIndex === -1) {
aSelectedDagdelen.push(sId);
oModel.setProperty("/aSelectedDagdelen", aSelectedDagdelen);
}
} else {
if (iIndex !== -1) {
aSelectedDagdelen.splice(iIndex, 1);
oModel.setProperty("/aSelectedDagdelen", aSelectedDagdelen);
}
}
if (fnCallback) {
fnCallback(oController);
}
},
enabled: {
path: "oModelZAPRegistratieMeta>/bChanged",
formatter: function(oFC) {
return oFC !== true;
}
},
width: "120px"
});
oPanel.addContent(oCheckBox);
}
addCheckBox("Ochtend", "O", true);
addCheckBox("Middag", "M", true);
addCheckBox("Avond", "A");
addCheckBox("Nacht", "N");
return oPanel;
}
});
The reason that the model doesn´t trigger a change event is that the reference to the Array does not change.
A possible way to change the value is to create a new Array everytime you read it from the model:
var newArray = oModel.getProperty("/aSelectedNumbers").slice();
// do your changes to the array
// ...
oModel.setProperty("/aSelectedNumbers", newArray);
This JSBin illustrates the issue.

Promise.map not finishing because subsequent Promise.join finishes first? Promise.all?

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...});
Yes that can be massively simplified, although your problem was probably just forgetting to return anything to the first mapper (and the massive wasteland of useless fulfillmentValue code).
var reqItems = req.body.items;
var items = Promise.map(reqItems, function(element) {
return Models.Item.findOneAsync({ "label": element }).then(function(item) {
if (item != null) {
return item;
} else {
return Models.Tag.findOneAsync({ "label": element }).then(function(tag) {
if (tag == null) {
var newTag = new Models.Tag({label: element});
return newTag.saveAsync().then(function() {
return Models.Tag.findOneAsync({ "label": element });
})
}
return tag;
}).then(function(tag) {
var newItem = new Models.Item({
label: element,
tag: tag._id
});
return newItem.saveAsync().then(function() {
return Models.Item.findOneAsync({ "label": element });
});
})
}
});
});
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, items, function(receivers, senders, items) {
store.receivers = receivers;
store.senders = senders;
store.items = items;
return store.saveAsync().return(store);
}).then(function(store) {
console.log("saved store", store);
res.json(store);
}).catch(Promise.OperationalError, function(e) {
console.log("error", e);
res.send(500, "error");
});

Categories