mariadb 尝试显示行数

h5qlskok  于 2023-03-30  发布在  其他
关注(0)|答案(1)|浏览(143)

我试图在html卡片中显示列的行数,我使用ejs插入数据
这是插入的代码

app.get('/home', async (req, res) => {
  const result = await db.query('SELECT COUNT(DISTINCT (unidadeconsumidora)) FROM unidadeconsumidora');
  console.log('result:', result);
  const ucCount = result.rows[0].count;
  console.log('ucCount:', ucCount);
  res.render('home', { ucCount: ucCount });
});

但未在控制台中显示任何信息
这是我得到的错误
C:\DEV\crm\server\src\app.js:378 const ucCount = result.rows[0].count;^
TypeError:Cannot read properties of undefined(阅读'0')at C:\DEV\crm\server\src\app.js:378:30 at process.processTicksAndRejections(node:internal/process/task_queues:95:5)

soat7uwm

soat7uwm1#

结果似乎是一个数组而不是对象!
为列示例count给予一个别名,然后必须使用result[0][0].count捕获它。

app.get('/home', async (req, res) => {
  const result = await db.query('SELECT COUNT(DISTINCT (unidadeconsumidora)) as count FROM unidadeconsumidora');
  console.log('result:', result[0]);
  const ucCount = result[0][0].count;
  console.log('ucCount:', ucCount);
  res.render('home', { ucCount: ucCount });
});

相关问题