Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

484
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!