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

455
Views
Cannot read properties of undefined (reading 'split'), how to remove error

When adding some fields to the page (from the client side), an error occurs: Cannot read properties of undefined (reading 'split'). Occurs in this code snippet const jwt = require('jsonwebtoken');

module.exports = function(role) {
    return function (req, res, next) {
        if (req.method === "OPTIONS") {
            next();
        }
        try {
            const token = req.headers.authorization.split(' ')[1];
            if (!token) {
                return res.status(401).json({message: "Not authorized"});
            }
            const decoded = jwt.verify(token, process.env.SECRET_KEY);
            if (decoded.role !== role) {
                return res.status(403).json({message: "No access"});
            }
            req.user = decoded;
            next();
        } catch (e) {
            console.log(e.message); //error message here
            res.status(401).json({message: "Not authorized"});
        }
    };
}

Moreover, if you add it from the server side (for example, in postman), then everything works well. Where is the mistake?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This simply means that req.headers.authorization value is not a string (or most likely it has the value: undefined. You should check first if the value is set (and that it is valid i.e a string) before calling split

if(req.headers.authorization) { const token = req.headers.authorization.split(' ')[1]; }

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!