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

215
Vistas
Global variable for GET all routes METHOD is not working in nodejs after sign in

I've made a blog page with a sign up and sign in. So I had used the req.locals.<name> method
With the GET method.

Code in server.js

app.get('*',(req,res, next) => {
  res.locals.user = req.user || null;
  next();
})

And I used the user variable in a pug page's navbar like :-

nav.navbar.navbar-expand-lg.navbar-light.bg-light
  .container
    a.navbar-brand.text-center.me-2(href='/') IA INFO
    button.navbar-toggler(type='button' data-bs-toggle='collapse' data-bs-target='#navbarSupportedContent' aria-controls='navbarSupportedContent' aria-expanded='false' aria-label='Toggle navigation')
      span.navbar-toggler-icon
    #navbarSupportedContent.collapse.navbar-collapse.justify-content-end
      ul.nav.nav-tabs
        li.nav-item
          a.nav-link(aria-current='page' href='/') Home
        li.nav-item
          a.nav-link(href='/blogs') Blogs
        if user
            li.nav-item
                a.nav-link(href='/blogs/add/new-blog') New Blog
            li.nav-item
                a.nav-link(href='/blogs/admin/sign') Sign
            li.nav-item
                a.nav-link(href='/logout') Logout

But when i try this method it doesn't really work....
After anyone logs-in, it is supposed to work but then it shows me an error of user is not defined or it just doesnt work...

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Instead of setting user as null, try setting a boolean inside the user object, more so like:

app.get('*',(req,res, next) => {
  res.locals.user = req.user ? {...req.user,...{isSignedIn : true} : {isSignedIn : false}; //spread operator has shortcomings, please use it with care 
  next();
})

and then in the pug file:

nav.navbar.navbar-expand-lg.navbar-light.bg-light
  .container
    a.navbar-brand.text-center.me-2(href='/') IA INFO
    button.navbar-toggler(type='button' data-bs-toggle='collapse' data-bs-target='#navbarSupportedContent' aria-controls='navbarSupportedContent' aria-expanded='false' aria-label='Toggle navigation')
      span.navbar-toggler-icon
    #navbarSupportedContent.collapse.navbar-collapse.justify-content-end
      ul.nav.nav-tabs
        li.nav-item
          a.nav-link(aria-current='page' href='/') Home
        li.nav-item
          a.nav-link(href='/blogs') Blogs
        if user.isSignedIn //check for the boolean
            li.nav-item
                a.nav-link(href='/blogs/add/new-blog') New Blog
            li.nav-item
                a.nav-link(href='/blogs/admin/sign') Sign
            li.nav-item
                a.nav-link(href='/logout') Logout

Read more about spread operators here

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