One issue I am facing is related to a "Product" class that I use for creating and displaying products on my website. The constructor of this class looks like this:
constructor(title, price, description, imageUrl, id)
. To update an existing product with new information provided through a form on an "update product" page using a POST route in Express, I have the following code:
exports.postEditProduct = (req, res, next) => {
const prodId = req.body.productId,
updatedTitle = req.body.title,
updatedPrice = req.body.price,
updatedImageUrl = req.body.imageUrl,
updatedDesc = req.body.description;
const product = new Product(
updatedTitle,
updatedPrice,
updatedDesc,
updatedImageUrl,
ObjectID(prodId) // Using mongodb!
); //additional promise-related code follows...
Thinking ahead, if I had to update a thousand properties manually, it would be a tedious task. This is where I'm looking for a solution - passing the updated props to the const product
constructor without individually naming each one. Although defining variables in this manner is not ideal and is just for experimentation purposes, it's something I don't plan on implementing in production.