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

为何我的pool.query返回"undefined"?数据库查询解构问题咨询

问题说明与现象

第一个查询可正常返回rows:

(async () => {
  const {rows} = await pool.query('SELECT * FROM accounts');
  console.log(rows);
})();

但以下查询返回undefined:

(async () => {
  const {user} = await pool.query(`SELECT * FROM accounts WHERE username = '${req.body.username}'`);
  console.log(user);
})();

已知req.body.username参数正确,该查询理应返回1行数据。如果去掉user外层的解构语法{},改用const user而非const {user},就能得到包含字段定义的完整返回结果:

(async () => {
  const user = await pool.query(`SELECT * FROM accounts WHERE username = '${req.body.username}'`);
  console.log(user);
})();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 13:52:02