V
V
Valery Orlov2019-02-07 00:46:32
JavaScript
Valery Orlov, 2019-02-07 00:46:32

How to solve the error Error: Route.post() requires a callback function but got a [object Object]?

Good day.
When calling route: router.post('/', upload, controller.app)
the following error occurs:

Error: Route.post() requires a callback function but got a [object Object]

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

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

module.exports = router

middleware/upload.js
const multer = require('multer')
const aws = require('aws-sdk')
const multerS3 = require('multer-s3')
const Events = require('../models/Events')

const errorHandler = require('../utils/errorHandler')
const keys = require('../config/keys')
const s3FileURL = keys.AWS_URL;

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);

      try {
        const updateFile = await Events.findOneAndUpdate(
            {rid: req.body.rid},
            {$set: { filename: s3FileURL+path }}
        )
        res.status(200).json({
          message: 'ok'
        })
      } catch (e) {
        errorHandler(res, e)
      }

    }
  })
}).any();

module.exports.upload = multer({upload})

controllers/app'
const mongoose = require('mongoose')
const User= require('../models/User')
const errorHandler = require('../utils/errorHandler')

module.exports.app = async function(req, res){
switch(req.body.user) {
        case 'email':
          const userEmail = await User.findOne({email: req.body.email}).select('_id')
             if (userEmail) {
              res.status(200).json({
                message: 'User find'
              })
             }
}
}

As I understand it, middleware is defined crookedly: upload.
How to get rid of this error?
I would be grateful for any criticism and advice on the code!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Shumov, 2019-02-07
@inoise

Several questions without reading the code. Starting with why not use a CDN for statics and ending with why the hell are you using keys in the code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question