feat: added getSimilarProducts
This commit is contained in:
1 parent
155fb60ec3
commit
e29d28ac18
1 file changed
+16
@@ -71,4 +71,20 @@ export async function getAllProducts() {
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
export async function getSimilarProducts(productId: string) {
|
||||
try {
|
||||
connectToDB();
|
||||
|
||||
const currentProduct = await Product.findById(productId);
|
||||
|
||||
if (!currentProduct) return null;
|
||||
|
||||
const similarProducts = await Product.find({ _id: { $ne: productId } }).limit(3);
|
||||
|
||||
return similarProducts
|
||||
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user