I have a frontend app(react) where the user can edit the product like price, description, delete images and add images. The problem occurs when the user does not want to add new images.
Then on the backend(nodejs) req.files.newImages as I named it is null and the backend crashes.
When I add one image or more everything works as it should because req.files.newImages has a value.
How to cover the case when the user does not want to add new images and prevent the app from crashing
I use express-fileupload and I use axios and put method to send data.
routes.put("/edit-product", (req, res) => {
console.log(req.files.newImages) // - result app crashed
}
Error in console: TypeError: Cannot read property 'newImages' of null;
CodePudding user response:
You can simply check if there are any files like this:
if (req.files) {
// do something
}
Alternatively you can use optional chaining:
console.log(req.files?.newImages); // will log "undefined"
CodePudding user response:
first check if it comes with console.log(req.files)
with another name, if not, make sure you send it as "multipart" on the front-end side