Commit 8d8a98af authored by Jiwon Yoon's avatar Jiwon Yoon
Browse files

asd

parent dcc19277
...@@ -71,28 +71,32 @@ const recommendPro = async (req, res) => { ...@@ -71,28 +71,32 @@ const recommendPro = async (req, res) => {
_id: "$products.productId", _id: "$products.productId",
count: { $sum: 1 } count: { $sum: 1 }
} }
},
{
$sort: {
count: -1
}
},
{ $limit: 5 },
{
$lookup:
{
from: "products",
localField: "_id",
foreignField: "_id",
as: "product"
}
} }
]) ])
console.log('recommend=', recommend) console.log('recommend=', recommend)
const filteredRecommend = recommend.filter((el) => String(el._id) !== String(productId)) const filteredRecommend = recommend.filter((el) => String(el._id) !== String(productId))
console.log('filtering=', filteredRecommend) console.log('filtering=', filteredRecommend)
filteredRecommend.sort(function (a, b) { // const array = filteredRecommend.map(async (el) => {
if (a.count > b.count) { // const aa = await Product.findById(el._id)
return -1; // return aa
} // })
if (a.count < b.count) { // const bb = await Promise.all(array)
return 1; res.json(filteredRecommend)
}
// a must be equal to b
return 0;
});
const finalrecommend= filteredRecommend.slice(0,4)
const array = finalrecommend.map(async (el) => {
const aa = await Product.findById(el._id)
return aa
})
const bb = await Promise.all(array)
res.json(bb)
} catch (error) { } catch (error) {
console.log('error in order ', error) console.log('error in order ', error)
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment