0

Here is the complete image

I need the result to Store in session. Is that easly Possible?

This is My Route,

app.get('/employ',(req,res) =>{

var q="select * from employee"
connection.query(q,(err,result) => {
    if (err) throw err;
    console.log("fetched values successfully...");
})

console.log(result);

res.render("addemploy.hbs")
})

If yes, mention how to access stored values from session anytime.

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
Sankar
  • 19
  • 1
  • 8
  • I tried, now data is Null. – Sankar Oct 27 '22 at 06:33
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Evert Oct 27 '22 at 09:41
  • @mrobbizulfikar How would that help? the data would still be null the millisecond after the query statement was executed but before it had finished. That is the point/problem with async – mplungjan Dec 15 '22 at 07:21

1 Answers1

2

You are using results out of the scope block (query), move the console.log(results) inside the scope of connection.query. If you wish to use the results in your template as well, render the page inside the scope as well. Make sure to render another page in case of an error in your query.

app.get('/employ',(req,res) =>{
    const q = "select * from employee";
    connection.query(q,(err,result) => {
        if (err) return res.send(err);
        console.log("fetched values successfully...");
        console.log(result);

        res.render("addemploy.hbs", { data: result });
    });
});
node_modules
  • 4,790
  • 6
  • 21
  • 37