I’m building a simple webshop, with a list of products. Now when I update products in the database, it doesn’t show the change on the webpage. I’m looking for some equivalent of await fetch(URL, {cache: 'no-cache'})
for mongoDb requests to solve this. How would this work in database requests?
This is my ProductList
:
export default async function ProductList() {
const products = await getProducts();
if (products === undefined) {
return <CircularProgress />;
}
if (products.length == 0) {
return <Typography>No products found</Typography>;
}
return (
<GridContainer sx={{ my: 4 }}>
{products.map((item) => (
<GridItem key={item._id?.toString()}>
<ProductCard product={item} />
</GridItem>
))}
</GridContainer>
);
}
This uses the getProducts()
function:
export default async function getProducts(amount = 10): Promise<Product[]> {
try {
const db = client.db(process.env.DB_NAME);
const product = await db
.collection(process.env.COL_PRODUCTS ?? "")
.find({ type: "Product" })
.limit(amount)
.toArray();
return JSON.parse(JSON.stringify(product));
} catch (e) {
console.error(e);
return [];
}
}