I will start with a small description of the result I want to get:
Let's imagine we have a collection called "Elements" which contains the 4 documents: 'a','b','c' and 'd'. I want to iterate over "Elements" and insert in a new collection called "Queries" the pairs :
(a,b);(a,c);(a,d);(b,a);(b,c)...(d,a);(d,b);(d,c). => which means "Queries" will contain (in this example) 4*3 = 12 pairs of elements (documents) at the end.
Here is the code I'm using (it's a method in meteor server triggered by a click on a button):
'Queries': function() {
var allElements = Elements.find();
allElements.forEach(function(myLeftElement){ //First forEach
allElements.forEach(function(myRightElement){// Nested forEach
if(myLeftElement._id !== myRightElement._id){
Queries.insert( {myLeftElement : myLeftElement._id, myRightElement : myRightElement._id} );
}
}); //End Of nested forEach
}); //End Of First forEach
}
In fact it works only for the first "myLeftElement" with all other elements "myRightElement" but stops there: it inserts, into "Queries" collection, the pairs: [(a,b);(a,c) and (a,d)] and then stops.
Since I am a beginner in web developement in general and in using Meteor in particular, I am seeking for your help.
1) I need to understand why once the nested cursor method "forEach" stops, the whole function stops.
2) How can I improve the code to get the result I really want: to every element of my collection "myLeftElement" there is a forEach method that creates pairs with all other elements "myRightElement". And then moves to the next "myLeftElement" and do the same till the end.
Thanks,