I am new to Node.js and I have recently encountered a very simple problem.
I use a module called multer, so users can upload images. In my web application, all users have a unique identifier, and I want the downloaded images to be stored in a directory with a name based on their identifier.
Example:
.public/uploads/3454367856437534
Here is part of my routes.js file:
// load multer to handle image uploads var multer = require('multer'); var saveDir = multer({ dest: './public/uploads/' + req.user._id, //error, we can not access this id from here onFileUploadStart: function (file) { return utils.validateImage(file); //validates the image file type } }); module.exports = function(app, passport) { app.post('/', saveDir, function(req, res) { id : req.user._id, //here i can access the user id }); }); }
How can i access
req.user._id
out of
function(req,res)
So, can I use it with multer to create the corresponding id based directory?
EDIT Here's what I tried and didn't work:
module.exports = function(app, passport) { app.post('/', function(req, res) { app.use(multer({ dest: './public/uploads/' + req.user._id })); }); }
source share