Someone help me, I am doing this for the first time and got stuck with this problem when sending POST request i’m getting this error…I think i’m not getting the file req.object was giving name & price but not the image
here’s the POSTMAN snapshot & "error": {
"message": "Cannot read properties of undefined (reading ‘path’)"
}
const Express = require('express');
const mongoose = require('mongoose');
const Product = require('../models/products');
const multer = require('multer');
const router = Express.Router();
const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, './uploads/');
},
filename: function(req, file, cb) {
cb(null, new Date().toISOString() + file.originalname);
}
});
const fileFilter = (req, file, cb) => {
if (file.mimetype === ['jpeg', 'jpg', 'png']) {
cb(null, true);
}
else {
cb(null, false);
}
}
const upload = multer({storage: storage, limits: {
fileSize: 1024 * 1024 * 5 },
fileFilter: fileFilter
});
//GET Request for all entries
router.get('/', (req, res) => {
Product.find().limit(100)
.select('name price _id productImage')
.exec()
.then((doc) => {
if (doc) {
console.log(doc);
res.status(200).json(doc);
} else {
res.send('Found 0 Products');
}
})
.catch((err) => {
console.log(err);
res.status(500).json({error: err});
})
});
//GET ID Wise
router.get('/:productId', (req, res) => {
let id = req.params.productId;
Product.findById(id)
.select('name price _id productImage')
.exec()
.then((doc) => {
if (doc) {
console.log(doc);
res.status(200).json(doc);
} else {
res.send('No valid entry found with this productID');
}
})
.catch((err) => {
console.log(err);
res.status(500).json({error: err});
});
});
//POST Request
router.post('/', upload.single('productImage'), (req, res) => {
console.log(req.body)
// if (!req.file) {
// return res.send('please upload the file');
// }
const product = new Product({
_id: new mongoose.Types.ObjectId(),
name: req.body.name,
price: req.body.price,
productImage: req.file.path
});
product.
save().
then(result => {
console.log(result);
res.status(200).json(result);
}).
catch( (err) => {
console.log("error while POST: " + err);
res.status(500).json(err);
});
});
//PATCH for updating
router.patch('/:productId', (req, res) => {
const id = req.params.productId;
Product.findByIdAndUpdate(id, {$set: req.body}, {new: true}).
exec().
then((result) => {
res.status(200).json(result);
}).
catch((err) => {
res.status(500);
res.send(err);
});
});
//DELETE for removing ID Wise
router.delete('/:productId', (req, res) => {
let id = req.params.productId;
Product.remove({_id: id}).exec().then(() => {
res.send(`${req.params.productId} got deleted successfully!`);
}).catch(
(error) => {
res.status(500).json(
{
error: error
}
);
}
);
});
module.exports = router;
product model
const mongoose = require('mongoose');
const productSchema = new mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: String,
price: Number,
productImage: String
});
module.exports = mongoose.model('Product', productSchema);
2
Answers
mistake found 😵💫
Debug
From the error message:
"message": "Cannot read properties of undefined (reading 'path')"
-> Somewhere in the code, we read the property
path
ofundefined
-> Scan your code, the place where we do that is when you create a new Product in this block:
->
req.file
isundefined
. What could be the caused? ThefileFilter
function is 1 place to look at. Here it is:Now you can spot the problem in this line
if (file.mimetype === ['jpeg', 'jpg', 'png']) {
. The mime-type can be eitherjpeg
,jpg
,png
… It cannot equal the array. So the condition always returns False -> the file is filtered out ->req.file
isundefined
-> the error.Action
You need to fix the condition to check if the mime-type is in the allowed values. 1 solution is:
if (['jpeg', 'jpg', 'png'].includes(file.mimetype))