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

254
Visualizações
Why is the ternary operator defaulting to true

I have the following bit of code:

const production = process.env.PRODUCTION
console.log(production)

const corsOptions = {
    origin: production ? 'https://myproductionurl' : 'http://localhost:3000',
    credentials: true
}

console.log(corsOptions)


app.use(cors(corsOptions))

When I run this code the first console.log is returning false as desired however the second console.log statement is returning the production URL as if the variable is true.

Not sure why this is if someone spots something then please let me know!

Furthermore, if I remove the ternary and URLs to test what's going on then i will get origin: false in the corsOptions which is stumping me even more!

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

0

Environment variables are always strings, unless they aren't set in which case they will be undefined.

If it is logging false, then you have set it to the string "false" which is a truthy value.

You can test for that explicitly:

production !== 'false' ? 'https://myproductionurl' : 'http://localhost:3000',

… although you should probably write a test that is more robust than that such as:

const isProduction = () => {
    const env = process.env.PRODUCTION; 
    if (!env) return false;
    if (env === "false") return false;
    return true;
}

and then

isProduction() ? 'https://myproductionurl' : 'http://localhost:3000',
about 4 years ago · Juan Pablo Isaza Relatório

0

Update your ternary logic

const env = process.env.PRODUCTION
console.log(env)

const corsOptions = {
    origin: env == "production" ? 'https://myproductionurl' : 'http://localhost:3000',
    credentials: true
}

console.log(corsOptions)


app.use(cors(corsOptions))
about 4 years ago · Juan Pablo Isaza Relatório

0

Because all environment variables are strings. And string that is false does not have boolean value of false, therefore the ? just see string that contains some characters => therefore resolving it as true.

const production = 'false'
const productionBool = false
console.log(production)

const corsOptions = {
    origin: production ? 'https://myproductionurl' : 'http://localhost:3000',
    originBool: productionBool ? 'https://myproductionurl' : 'http://localhost:3000',
    credentials: true
}

console.log(corsOptions)

You can resolve it i.e. by (String(production).toLowerCase() === 'true')

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