I am creating an e commerce mobile app using React Native, within this I have a server, the role of the server is currently to get products from the woocommerce APi, take only the fields I need : name, id, category,price, image and then add another field called 'favourite'(for my wishlist).
Everything was worked well until the getProducts method has a method that calls the woocommerce API and then pushes that to the database. Problem is that whenever I try to update an item to 'true' rather than 'false' within the favourite field. It populates but when I refresh the page which also recalls the API, it brings the 'favourite' field back to false, which it was initially initialised as in the server.
I present the code in question:
app.get("/getProductsForUser", async (req, res) => {
const productDetails = {};
const numberOfProducts = 25;
api
.get("products", {
per_page: numberOfProducts, // 20 products per page
})
.then((response) => {
// Successful request
var key = "Products";
productDetails[key] = [];
var data;
for (let index = 0; index < numberOfProducts; index++) {
productDetails[key].push({
id: response.data[index].id,
name: response.data[index].name,
image: response.data[index].images[0].src,
price: response.data[index].price,
favourite: false,
category: response.data[index].categories[0].name,
});
}
db.collection("users")
.doc(req.query.userID)
.collection("products")
.doc("0")
.set({ productDetails });
res.send(productDetails);
})
.catch((error) => {
// Invalid request, for 4xx and 5xx statuses
console.log("Response Status:", error);
})
.finally(() => {
// Always executed.
});
});
If there is a way I can only call the above function once in the whole project (which is in nodejs by the way) and then after that only call from the database, that will help?