Hello together,
I had a few weeks now with the new Multer Release/API and it works well for me so far. Thanks for optimizing it and making it better.
Just one thing in the new API feels a bit strange to me when using (f.e.) the disk storage .
cb(null, '/tmp/my-uploads')
It confuses me a bit to explicitly pass a null value. Is it important here? Can't we make that parameter just optional? We could somehow still throw / pass an Error if something goes wrong. Do we have control over it, or does it maybe relate to the "next()" express middleware call?
At least I would expect a small hint in the documentation what this null value is all about. When I used the disk storage for the first time, I was a bit confused about this. Later on in the documentation I found
cb(new Error('I don\'t have a clue!'))
and things were a bit more clear for me, but it's still a different part in the documentation
Regards
Rolf
This is a very fundamental part of Node.js and it's part of every function in the standard api. You are probably used to seeing the other side of it thought.
Consider the following example for reading a file.
fs.readFile('/tmp/linus.txt', function (err, raw) {
if (err) throw err
console.log('File contains:', raw.toString())
})
err is always the first parameter of a callback, indicating if an error occurred or not. Since you are now implementing the other side, you'll be the one that actually supplies the error and the result; hence cb(null, 'result').
I really recommend reading Error Handling in Node.js, it should tell you all about this.
Personally I don't think that we need to document this further since it's such a fundamental part of Node.js. Please tell me if you think I'm wrong thought! :+1:
Thanks for providing me with the link and clarifying it :)
Well at least for me (with the given examples) it was not clear enough at the time reading it.
You are way deeper in the topic than I am, but I think a small hint would be nice.
I know it's not your job to teach people how to code properly, but since Multer is one of the easiest ways to create a file upload server in NodeJS, it could really help people if you just refer to the link you just gave me :) (like "See XXX for common error handling")
Hmm, yes that would actually be smart, thank you for your feedback :100:
You can get the saved path by doing req.file.path
You can get the saved path by doing
req.file.path
i am trying to log req.file.path, but is undefined. maybe you can explain more please
Hey there! Currently stuck with the same issue. Also trying to return req.file.path but it returns undefined.
Would appreciate some clearance! :)
@darmawan01 @4dams could you both add code samples to help with debugging? req.file.path should be available. Here's a working example:
server:
const express = require('express');
const multer = require('multer');
const upload = multer({ dest: 'files/' });
const app = express();
app.use(express.static('public'))
app.post('/file', upload.single('file'), (req, res, next) => {
console.log(req.file.path);
res.send(req.file);
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`App is listening on port ${PORT}`));
html form:
<form action="file" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="file">
<input type="submit" value="Upload Image" name="submit">
</form>
I'm going to go ahead and close this issue. @darmawan01 & @4dams feel free to open new issues with your specific problems if you're still facing them.
Most helpful comment
This is a very fundamental part of Node.js and it's part of every function in the standard api. You are probably used to seeing the other side of it thought.
Consider the following example for reading a file.
erris always the first parameter of a callback, indicating if an error occurred or not. Since you are now implementing the other side, you'll be the one that actually supplies the error and the result; hencecb(null, 'result').I really recommend reading Error Handling in Node.js, it should tell you all about this.
Personally I don't think that we need to document this further since it's such a fundamental part of Node.js. Please tell me if you think I'm wrong thought! :+1: