Level 63
I don't know knex.
But it looks like you are creating a new product (that means with a null id) to update it (which has no sense to try to update a new product).
To update a product, you should retrieve an existing product.
In your code, I don't see any place where you initialize the id from the retrieved product.
const updateProduct = async (newProduct) => {
const{
id, // new product, so a null id
name,
price,
stock,
description
}= newProduct
try {
const product = await knex("products").where('id',id).update({ // so here the id is null
'name':name,
'price':price,
'stock':stock,
'description':description
});
return product;
} catch (error) {
console.log(error);
console.error(error)
throw new Error("Failed to update the product");
}
};