Be displaying the data on the front end with Node.js

Asked

Viewed 94 times

-1

I’m having a problem where basically when inserting the data of two mysql tables in front-end I’m not getting, I think I’m reporting in the wrong way.

    // Exibir post
    app.get('/cad', function(req, res){
      Aluno.findAll({order: [['id', 'DESC']]}).then(function(alunos){
          res.render('formulario', {alunos: alunos})
      })
  })
    app.get('/cad', function(req, res){
        Post.findAll({order: [['id', 'DESC']]}).then(function(posts){
            res.render('formulario', {posts: posts})
        })
    })

I tried this way and a few more, as shown above, sending the data prevails who comes first, in case I wanted to be sending the two at the same time, so I can be displaying the database information on the front end. Carrying out this process would be possible?

1 answer

1


It is possible to achieve the expected result using the following standard.

From what I understand, you want to display the result of two tables on the same web page.

Using your code as an example, just do the following:

app.get('/cad', function (req, res) {
    Aluno.findAll({ order: [['id', 'DESC']] }).then(function (alunos) {
        const alunosdata = alunos
        Post.findAll({ order: [['id', 'DESC']] }).then(function (posts) {
            res.render('formulario', { posts: posts, alunos: alunosdata })
        })
    })
})
  • Just one question, I may be including more tables to be displayed this way?

  • 1

    Yes, just add to the json in function render.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.