You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Express.js中pool.query()数据无法渲染至EJS页面的问题

问题:Express.js中如何将pool.query获取的数据渲染到EJS页面?

我正在用Express.js开发一个简单应用(此问题属于Express.js语法问题,与questions/23667086不同,故单独提问)。现在遇到的问题是,通过pool.query()从数据库获取的数据只能在其回调内部访问,无法在router.get("/synoptyka")的回调外部获取,而且没法把数据渲染到index.ejs页面。

初始代码

app.js

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}`) // 输出数据: [ [ { adr_adm: 'Palmowa 26', adr_sym: 'Palmowa_26' } ] ]
            resp.render('index', {data: dane}) // 数据无法渲染到index页面
            pool.end()           
            
        }
    })
})
module.exports = router

server.js

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

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

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

index.ejs

<!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>

请问如何将pool.query()获取的数据渲染到index.ejs页面?是否需要将数据库数据写入外部存储?

修改后可正常运行的app.js代码

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  

问题原因与解决方法

  1. 核心问题:初始代码里把res.rows(本身就是包含查询结果对象的数组)push到了dane数组中,导致传递给EJS的data变成了二维数组[[{...}]]。而EJS模板直接遍历data并访问元素的属性,但二维数组的元素是子数组而非对象,自然无法获取到属性值,导致渲染失败。
  2. 解决方式:直接将res.rows作为data参数传递给resp.render()即可,因为res.rows已经是符合EJS遍历要求的一维对象数组。
  3. 外部存储必要性:不需要。数据库查询结果可以直接在pool.query的回调内部传递给渲染函数,Express的异步响应流程支持这种处理方式,完全没必要写入外部存储。

内容的提问来源于stack exchange,提问作者Bear4

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 04:55:23