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

187
Views
res.locals don't work in views - undefined

need help. My res.locals.... dont work in views. In app.js i get a true but in views i get undefined. Any idea to solve my problem?!

thats my code snippet from app.js

app.use(async function(req, res, next) {
  const user = req.session.user;
  const isAuth = req.session.isAuthenticated;

  if (!user || !isAuth) {
  return next();
  }

  const userDoc = await User.findOne({_id: user.id});
  const isAdmin = userDoc.isAdmin;

  res.locals.isAuth = isAuth;
  console.log(res.locals.isAuth);
  res.locals.isAdmin = isAdmin;
  next();
});


app.use('/users', userRoutes);
app.use('/', coinRoutes);
app.use('/', marketRoutes);
app.use('/', footerRoutes);
app.use('/', holdingRoutes);

thats my code snippet from navbar.ejs

<nav class="mdl-navigation mdl-layout--large-screen-only">
    <a class="mdl-navigation__link" href="/">Top 250</a>
    <a class="mdl-navigation__link" href="/topflop">Top/Flop Coins</a>
    <a class="mdl-navigation__link" href="/top">Top gesuchte Coins</a>
    <a class="mdl-navigation__link" href="/markets">Börsen</a>
    <a class="mdl-navigation__link" href="/holdings"
      >BTC-Unternehmen</a
    >
    <a class="mdl-navigation__link" href="/info">News</a>
    <% if(locals.isAdmin) { %>
    <a id='admin' class="mdl-navigation__link log" href="/users/admin">Admin</a>
    <% } %>
    <% if (!locals.isAuth) { %>
      <% console.log(locals.isAuth + 'Hello') %>
    <a id='sign' class="mdl-navigation__link" href="/users/signup">Signup</a>
    <a id='login' class="mdl-navigation__link" href="/users/login">Login</a>
    <% } else { %>
    <form id="logout-form" action="/users/logout" method="post">
      <button id="logout" type="submit">Logout</button>
    </form>
    <% } %> 
  </nav>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

in the view you need to call it directly like this

<% if ( !isAdmin) { %>
 ...
 <% } %>


<% if ( !isAuth) { %>
 ...
 <% } %>
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!