I'm trying to create an array of 12 unique products. These products are being selected from a list of products that are retrieved from my MONGODB Database. I can create the array quite alright, but I can't seem to get the length of the array to be 12 every time. I need the minimum length of the array to be 12 and the maximum length of the array to also be 12.
This is my code below and everytime I run this code, randomProducts.length is usually a random length, instead of 12.
Products.find({}).limit(12).exec(function (err, randProducts) {
var randomProducts = []
for (let i = 0; i < randProducts.length; i++) {
var rand = Math.floor(Math.random() * (randProducts.length - 0) + 0);
if (randomProducts.includes(randProducts[rand]) == false) {
randomProducts.push(randProducts[rand])
}
}
console.log(randomProducts.length)
})