Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

190
Visualizações
Logical operator "!" not working in ejs file

This is the code

<% if( !title || title!=="login"){   %>   
<div class="dashboard-main-cont">
    <div class="navigation-option">
        <a class="btn btn-primary navbtn" href="/admin/bookings">Booking Requests</a>
        <a class="btn btn-primary navbtn" href="/admin/showprojects">Manage Projects</a>
        <a class="btn btn-primary navbtn" href="#">Calculation Requests</a>
        <a class="btn btn-primary navbtn" href="#">Manage Blogs</a>
        <form action="/admin/logout" method="POST">
            <button class="btn btn-danger">Logout</button>
        </form>
    </div>
<% } %> 

I am not passing a title value but I have put "!" in front of "title", but I am still getting this error:

title is not defined

How to solve this problem? Thanks in advance!!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You probably should define title first using keywords like let or const. This error is coming because you have not defined it.

about 4 years ago · Juan Pablo Isaza Relatório

0

So, I think that "title" needs to at least be declared for it to be able to work with the "!".

You could render the file by passing title: undefined on the render information. That way, the variable is defined as "undefined", but at least it is defined.

On the snippet below, by defining the variable, it can be used by the bang to detect if it is a "positive" value, as for the second "if", it throws an error.

const declared_title = undefined

console.log("foo")

if( !declared_title || declared_title !== "login") {
    console.log("bar")
}

console.log("fizz")

if( !undeclared_title || undeclared_title !== "login") {
    console.log("buzz")
}

As for the render call, it should look something like this

res.render('your_page',{ title: undefined });

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda