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

493
Vistas
Property '_id' does not exist on type 'string | JwtPayload'. Property '_id' does not exist on type 'string'

As you can see in the NodeJS code I'm grabbing the user from the token from Postman provided in the URL but I'm struggling to get back the _id property from the given token number. What I want is that id in the decoded._id so that I can use that id in further operations

const jwt = require('jsonwebtoken')
const User = require('../model/users')
const auth = async (req, res, next) => {
    try {
          const token = req.header('Authorization').replace('Bearer', '')
          const decoded = jwt.verify(token, 'thisisfromabhishek')

`the property _id does not exist on decoded`
          const user = await User.findOne({ _id: decoded._id, 'tokens.token': token })

        if (!user) {
            throw new Error()
        }
        req.token = token
        req.user = user
        next()
       } catch (e) {
        res.status(401).send({ error: 'Please authenticate.' })
    }
}

module.exports = auth
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Typescript don't know about _id in token. Data declaration should help.

interface JwtPayload {
  _id: string
}

const { _id } = jwt.verify(token, 'thisisfromabhishek') as JwtPayload

req.user = await User.findOne({ _id, 'tokens.token': token })

about 4 years ago · Juan Pablo Isaza Denunciar

0

you have not given space after Bearer.

const token = req.header('Authorization').replace('Bearer', '') //here is the problem place space after Bearer
          const decoded = jwt.verify(token, 'thisisfromabhishek')

about 4 years ago · Juan Pablo Isaza 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