V
V
Valery Orlov2019-02-07 16:47:46
JavaScript
Valery Orlov, 2019-02-07 16:47:46

How to access req.files.location value of upload.any() object?

Good day!
Help to display in the controller the path req.files.location of object upload.any()
routes/app.js

const express = require('express')
const upload = require('../middleware/upload')
const controller = require('../controllers/app')
const router =  express.Router()

router.post('/', upload.any(), controller.app)

module.exports = router

middleware/upload.js
const multer = require('multer')
const aws = require('aws-sdk')
const multerS3 = require('multer-s3')
const errorHandler = require('../utils/errorHandler')
const keys = require('../config/keys')

aws.config.update({
  secretAccessKey: keys.ACCESS_KEY,
  accessKeyId: keys.ACCESS_KEY_ID,
  region: keys.REGION
})

const s3 = new aws.S3();

const upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'verstesting',
    acl: 'public-read',
    key: async function (req, file, cb) {
      const today = new Date().toISOString().slice(0, 10);
      const path = "files.devices/" + req.body.id + "/" + today + "/" + file.originalname + ".jpg";
      cb(null, path);
      console.log('UPLOAD')
    }
  })
})
module.exports = upload

/controller/app.js
module.exports.app = function(req, res){
 console.log(req.files.location)  // тут то и не возвращает
}

What is quite funny, if you call upload.single('image'), then the req.file.location construct successfully displays the path where the image was saved. As I understand it, upload.any() needs to be accessed somehow differently and req.files.location does not work.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question