0

I'm writing simple app in express.js

(my problem is differ than questions/23667086 - it's syntax express.js not pure JS - so I prefere to leave my question )

And I have problem with data availability just downloaded from db via pool.query()

I can't receive data:

outside -> router.get("/synoptyka", (req, resp, next) => {... ) - data not available

inside -> pool.query(queryDB , (err, res) => {... ) - data available

and my app.js code:

const express = require('express')
const router = express.Router()
const pool = require('../conn-pool-mapy')

 
router.get("/synoptyka", (req, resp, next) => {
    req.query
    console.log(req.query) // ({ par: 'Palmowa_26' })
    const dane = []
    queryDB = "SELECT adr_adm, adr_sym FROM public.v_synoptyka where adr_sym ='"+req.query.par+"'"

    pool.query(queryDB , (err, res) => {
        if (err) {
            // throw err;
            console.log('connection error')
            return
        }    
        if (res) {
            dane.push(res.rows) 
            console.log(`res: ${dane}`) // have data: [ [ { adr_adm: 'Palmowa 26', adr_sym: 'Palmowa_26' } ] ]
            resp.render('index', {data: dane}) // data don't render in index
            pool.end()           
            
        }
    })
})
module.exports = router

my server.js code:

const express = require('express');
const app = express();

app.set("view engine", "ejs");

const synoptRouter = require('./routes/app')
app.use('/', synoptRouter)
app.listen(5000);

my index.ejs code:

<!DOCTYPE html>
<html lang="pl">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
  <table>
    <thead>
        <tr>
        <th>adr adm</th>
        <th>adr sym</th>
        </tr>
    </thead>
    <tbody>
        <% data.forEach(function(item) { %>
        <tr>
            <td><%= item.adr_adm %></td>
            <td><%= item.adr_sym %></td>
        </tr>
        <% }); %>
    </tbody>
    </table>        
 </body>
 </html>

How to get data from pool.query() to render my index.ejs ?

Maybe should write data from DB into outside store?

After modification everything works proper:

const express = require('express')
const router = express.Router()
const pool = require('../conn-pool-mapy')    


router.get("/synoptyka", (req, resp, next) => {
    req.query
    console.log(req.query) // ({ par: 'Palmowa_26' })
    const dane = []

    queryDB = "SELECT adr_adm, adr_sym FROM public.v_synoptyka where adr_sym ='"+req.query.par+"'"

    pool.query(queryDB , (err, res) => {
        if (err) {
            // throw err;
            console.log('connection error')
            return
        }    
        if (res) {
            resp.render('index', {data: res.rows})  
            pool.end()           
        }
    })
})
module.exports = router  
Bear4
  • 17
  • 6
  • Don't call `next()` before you have the data you want (i.e. call it inside your callback). – Quentin Feb 01 '23 at 08:59
  • Also, don't use globals to pass data about. You're going to end up with `dane` containing the data from multiple requests. – Quentin Feb 01 '23 at 08:59
  • I use resp.render('index', {data: res.rows}) - so thanks my problem was resolved. – Bear4 Feb 01 '23 at 09:40

0 Answers0