I am doing recommend system function but i meet a problem like below
const getProductRecommmend = asyncHandler(async (req, res) => {
let arrMeasure = []
const comparePromise = (desc) => {
return new Promise((resolve,rejects) => {
tfidf.tfidfs(desc,function (i,measure) {
arrMeasure.push({
index:i,
measureProps:measure
})
})
resolve(arrMeasure)
})
}
const lastOrder = await (await Order.find({ user: req.user._id })).pop()
const products = await Product.find({})
// console.log(products)
products.forEach(item => {
tfidf.addDocument(item.description);
})
let arrResultMeasure = []
lastOrder.orderItems.forEach(async(item) => {
const product = await Product.findById(item.product).select('description')
const resultMeasure = await comparePromise(product.description)
arrResultMeasure = [...resultMeasure]
// console.log('arrResultMeasure',arrResultMeasure)
})
console.log('arr1',arrResultMeasure) // empty array
res.json(lastOrder)
})
When I console log arrResultMeasure inside forEach, it still log value, but when I console log outside of it, as you see in this below code, it return an empty array, I tried many way but it's still doesn't work.Does anyone have a solution to this problem? Thank you!