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

164
Visualizações
Node.js render alert

im having a little trouble getting an alert warning to show up on a page when the user inputs a string into a form that is less than some number of characters. The code is working except for actually displaying the error on the actual page. the main app.js has the following function to handle the input error

app.post('/register', 
  body('name').isLength({ min: 5 })
, (req, res)=> {
  const errors = validationResult(req)
  if(!errors.isEmpty()) {

      const alert = errors.array()

      app.get('/store', function(req, res){
        fs.readFile('items.json', function(error, data){
            if(error){
                res.status(500).end()
            }
            else {
                res.render('store.ejs', {
                    items: JSON.parse(data),
                    alert
                })
            }
        })
      })

      console.log('--> length error')
  }
})

The main app.js already has a (below) function to start the store page upon request also.

app.get('/store', function(req, res){ ...

in the store.ejs i have:

        <% if(typeof alert != 'undefined') { %>
            <% alert.forEach(function(error) { %>
                <div class="alert alert-warning alert-dismissible fade show" role="alert">
                    <%= error.msg %>
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                    </button>
                </div>
            <% }) %>
        <% } %>
about 4 years ago · Juan Pablo Isaza
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