I've created a loop to add products (with data) to different regions. To start with I want to add all products to all regions. I will then (in a different operation) remove products from these regions.
The premise of the site is that objects will be able to be reserved by a user in a region, that will make that object unavailable in that region but still available in other regions. I save the users region on sign up and only allow them to see the objects available in their region.
I have created objects called Regions and I'm adding each product to an array within regions. The reason I am storing them within regions is in the future I am expecting hundreds of different products and believe that just returning all the items within a region array will be much easier on the server than checking a value within each product individually.
My problem is that
when running my code, I am getting duplicates of each object within my page.
The code I am using is:
dummyregions.forEach(function(seed){
Region.create(seed, function(err, region){
if(err){
console.log(err);
} else {
dummyproducts.forEach(function(seedprod){
Product.create(seedprod, function(err, product){
if(err){
console.log(err);
} else {
region.products.push(product);
region.save();
}
});
});
}
})
});
dummyRegions is an object, containing a name "string" and an array = []
dummyproducts contains a name "string", category "string" and a thumbnail image url "string"
I only have 4 test items in dummy products and 3 regions, however this is the result I'm getting: Duplicate Items on each Region
Any help would be much appreciated!