DEV Community

puzzle29
puzzle29

Posted on

1

Problem with axios header in nuxt.js

Hello,

I'm working on a project using nuxt.js that other developers have already worked on.
I don't know much about nuxt.js and I'm having a problem that I can't solve.
When I do an npm run build then npm run start the application works normally, but when I do an npm run dev I get errors:

TypeError: Cannot read properties of undefined (reading 'Authorization')                         
TypeError: Cannot read properties of undefined (reading 'status')                                

 ERROR  Cannot read properties of undefined (reading 'status')                                           

  at server.js:13024:24
  at server.js:455:56
  at async asyncData (pages/landing/students.js:2156:35)
  at async Promise.all (index 0)
  at async Promise.all (index 0)
  at async module.exports../.nuxt/server.js.__webpack_exports__.default (server.js:5494:22)
Enter fullscreen mode Exit fullscreen mode

Moreover if I remove the headers from the axios requests, there is no more error and the application works when I do npm run dev.

Here is a portion of the code that is causing the problem :

async asyncData({app, params, $axios, $config: { baseURL } }) {
        const res = await $axios.$get(`/api/student`, {
            headers: {
              'Accept-Language': app.i18n.locale,
            },
        })
        .then((res) => {
          console.log(res)
          return res
        })
        .catch((err) => {
          console.error(err)
        });

        return { res }
    }
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay