Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

95
Vistas
Error while passing multiple middlewares in Edit Route

I am getting the following error when I try to pass multiple middlewares in my Edit route.

The error is:

Route.put() requires a callback function but got a [object Object]

This is the code:

const auth = require('../middleware/auth')
const admin = require('../middleware/admin')

router.put('/inflow/item/:id', [auth, admin],async (req, res) => {

    const brand = await Brand.findOne({ brand: req.body.brand })
    const existingItem = await Stocks.findById(req.params.id)
    
    const item = {
        'productname' : req.body.productname, 
        'quantity' : existingItem.quantity + req.body.quantity, 
        'brand' : brand._id,
        
    }

    const logChanges = {

        'quantityAdded' : req.body.quantity,
        'author' : 'Peter'
    }

    console.log(logChanges)

    const update = {
        $set : item,
        $push : {logAddition : logChanges}
    }

    console.log(item)

const stocks = Stocks.findByIdAndUpdate(req.params.id, update,{new: true}, (err, updateListing) => {
        if(err){
            res.status(404).send({success: false, msg: err.message})
        }   
        console.log(updateListing)
        res.json(updateListing)
    })

})

The error disappears when I remove one of the middlewares. Like this:

router.put('/inflow/item/:id', [auth, admin],async (req, res) => {
}

Below are the code snippets for auth and admin middlewares:

Auth



const jwt = require('jsonwebtoken')
const config = require('config')

module.exports = function (req, res, next){
    const token = req.header('x-auth-token')
    if (!token) return res.status(401).send('Access Denied. No token provided')
    res.status(401)
    try {
        const decoded = jwt.verify(token, config.get('jwtPrivatekey'))
        req.user = decoded 
        next() // we either terminate process or we pass control
    }catch(ex){
        res.status(400).send('Invalid Token', ex.message)
    } 
}

Admin

module.exports = function(req, res, next) {
    //401 Unauthorized - no json webtoken
    //403 Forbideen - you tried again but still cannot access 
    if(!req.user.isAdmin) return res.status(403).send('Access denied')

    next()
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Either you haven't shared the complete code of the edit route controller. Or you forgot to export the router object in the controller file.

router.put('/inflow/item/:id', [auth, admin],async (req, res) => {
}

// I think you've missed this line
module.exports = router;

Read more on defining and using separate route modules.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda