how to fetch multiple tables from multiple tables in a database using nodeJs Mysql ExpressJs?

Issue

I need some help to find where the problem is. i want to fetch this tables from different queries (tables in Mysql), gestionEntreprise.ejs:

<tbody>
       <tr>
           <% data1.forEach(entry => { %>
           <th scope="row"><%= entry.IDdirection %></th>
           <td><%= entry.emailCompteDirecteur %></td>
       </tr>
<% }) %>
</tbody>
<tbody>
      <tr>
          <% data2.forEach(entry => { %>
          <th scope="row"><%= entry.IDsousDirection %></th>
          <td><%= entry.IDdirection %></td>
      </tr>
<% }) %>
</tbody>
...

i have this request in app.js:

app.get('/gestionEntreprise',(req,res)=>{
const sql1 = 'SELECT * FROM direction';
const sql2 = 'SELECT * FROM sousdirection';
const sql3 = 'SELECT * FROM departement';
const sql4 = 'SELECT * FROM equipe';
getConn().query(sql1, (err, fetchdirection)=>{
    if(err){
        console.log('Failed to query ', err);
        return;
    }
    getConn().query(sql2, (err, fetchsousdirection)=>{
        if(err){
            console.log('Failed to query ', err);
            return;
        }
        getConn().query(sql3, (err, fetchdepartement)=>{
            if(err){
                console.log('Failed to query ', err);
                return;
            }
            getConn().query(sql4, (err, fetchequipe)=>{
                if(err){
                    console.log('Failed to query ', err);
                    return;
                }
                res.render('gestionEntreprise',{data1: fetchdirection,data2: fetchsousdirection,data3: fetchdepartement,data4: fetchequipe});
            })
        })
    })
})
res.end();
});

but it show me this error:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

where is the problem, and how to fix it?

Solution

What happens is that data that I select from the database are not ready, and nodeJs is framework asynchronous, so I add setTimeout to sleep the rendering process.

gestionEntreprise.ejs

app.get('/gestionEntreprise',async (req,res)=>{
const queryDirections = "SELECT * FROM direction";
const querySousDirections = "SELECT * FROM sousdirection";
const queryDepartments = "SELECT * FROM departement";
const queryEquipes = "SELECT * FROM equipe";
try {
    // Get connection once
    const conn = getConn();
    // Techniques: Array destructuring and Promise resolving in batch
    let directions, sousDirectios, departments, equipes;
    await Promise.all(
    [
        conn.query(queryDirections,(err, rows)=>{directions=rows;} ),
        conn.query(querySousDirections,(err, rows)=>{sousDirectios=rows;}),
        conn.query(queryDepartments,(err, rows)=>{departments=rows;}),
        conn.query(queryEquipes,(err, rows)=>{equipes=rows;}),
    ]
    );
    setTimeout(() => {
    res.render("gestionEntreprise", {
    data1: directions,
    data2: sousDirectios,
    data3: departments,
    data4: equipes,
    });},100);
} catch (error) {
console.log(error);
res.end();
}
});

Answered By – Bilal Belli

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published