Reputation: 255
I am wondering if in the following scenario am I am handling the right way the error and what is that I should return on error? Can you return statusCode on anything or only on response?
const storage = multer.diskStorage({
destination: function (req, file, cb) {
if (err) {
new Error({
status: "INTERNAL SERVER ERROR"
})
}
let filepath = './public/images/'
cb(null, filepath)
},
filename: function (req, file, cb) {
if (err) {
new Error({
status: "INTERNAL SERVER ERROR"
})
}
let ext = file.originalname.split(".").pop();
let filename = file.fieldname + '-' + Date.now() + '.' + ext
//console.log(ext);
cb(null, filename);
}
})
Upvotes: 0
Views: 1640
Reputation: 4983
You can use status codes only on the response object.
For more details read this.
Try reading this question once.
The answer to your updated code:
You can send the error in the callback object. Read more about callback here.
The callback takes two params:
I will update your code below:
Updated Code:
const storage = multer.diskStorage({
destination: function(req, file, cb) {
if (err) {
cb(err, null);
}
let filepath = './public/images/'
cb(null, filepath)
},
filename: function(req, file, cb) {
if (err) {
cb(err, null);
}
let ext = file.originalname.split(".").pop();
let filename = file.fieldname + '-' + Date.now() + '.' + ext
//console.log(ext);
cb(null, filename);
}
})
This is how you ideally handle errors with the callback.
Try this and check if it works.
Upvotes: 1