title: Access res.locals in Multer.diskStorage->filename
body: <p>I'm working on a login, where the use consists of userImage, userName, userStatus (all send as form-data).
  To upload the userImage im using Multer.
  This is how my route looks like</p>
  <p><code>router.post('/create', upload.any(), createUser, upload.single('userImage'));</code></p>
  <p><code>upload.any()</code> This im calling so that I can retrieve the userName &amp; userStatus from the form-data body, which can't be accessed via <code>req.body</code> otherwise</p>
  <p><code>createUser</code> Is my middleware to create the User which looks like this</p>
  <pre><code>        var savedUser = await user.save();
          var createdUserId = savedUser._id;  // use this to add to the profile Image's name 
  
          var imgExtension = path.extname(req.body.userImage)
          var imgPath = `./images/profileImages/${createdUserId}_profileImage${imgExtension}`
          
          //now update the newly created user with its image 
          await userModel.updateOne({_id: createdUserId},{
              image: {
                  path: imgPath,
                  contentType: `image/${imgExtension}`  
              }
          });
          // to use in Multer middleware
          res.locals.imgName = String(`${createdUserId}_profileImage${imgExtension}`)
  
          res.status(201).json({
              message: &quot;User has been saved&quot;,
              userId: createdUserId
          });        
          
          next()
  </code></pre>
  <p>So Im using mongoDB, after creating the user in the Database Im getting the ObjectID which I want to be included in the image name, only the path to the image is being saved on the server.</p>
  <p>Now I'm trying to pass the imgName as <code>res.locals.imgName</code> to the Multer middleware so that I can store it on the server.</p>
  <pre><code>var storage = multer.diskStorage({
      destination: (req, file, cb) =&gt;{
          cb(null, './images/profileImages')
      },
      filename: (req , file, cb) =&gt; {
          const imgName = req.res.locals.imgName // HERE LIES THE PROBLEM
          cb(null, `${imgName}`)
      }
  });
  export var upload = multer({ storage: storage});
  </code></pre>
  <p>[if there is a typo in the code that I missed it's only because I couldn't copy and paste it to the Stackoverflow code-field, nodejs needs ` &lt;- this symbol so that it can access variables in a String ]</p>
  <p>So on the Database everything works out fine, all the information is saved correctly.</p>
  <p>Now the Image is being saved on the server but as undefined, if I add a .png after the undefined I can also view the image.</p>
  <p>I need a way to access the <code>res.locals.imgName</code> variable in Multer filename.
  <a href="https://github.com/expressjs/multer/issues/620" rel="nofollow noreferrer">Here I found something similar</a></p>
  <p>one user said <code>res.locals.something</code> can be accessed via <code>req.res</code>, he didn't say anything else so I tried every combination of <code>req.res.res.locals.imgName</code> to <code>req.res.imgName</code> and it's always undefined or doesn't work altogether.</p>
  <p>So is there a way to solve this or do I have to MacGyver some workaround.</p>