Express JS returns 查询结果为 null 并且在使用 pgAdmin 查询时显示值
Express JS returns null for a query result and values are shown when queried with pgAdmin
我有以下查询国家名称列表的代码:
router.get('/', function(req, res) {
console.log("/");
const query =
`SELECT DISTINCT name
FROM countries`;
db.map(query, [], a => a.json)
.then(data => {
res.send({
data: data,
status: 200
});
})
.catch(error => {
console.log(error);
res.status(500).send('Error occured');
});
});
作为响应,我得到如下响应:
{
"data": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
........
],
"status": 200
}
但是当我使用 pgAdmin 运行 相同的查询时,我得到的是国家名称而不是 'null'。有人可以帮我弄清楚这个问题吗?我是初学者,不知道这里出了什么问题。
提前致谢。
您的查询正在选择字段 name
,但您正在使用名为 json
的 属性 映射结果。您应该执行以下操作:
db.map(query, [], a => a.name)
我有以下查询国家名称列表的代码:
router.get('/', function(req, res) {
console.log("/");
const query =
`SELECT DISTINCT name
FROM countries`;
db.map(query, [], a => a.json)
.then(data => {
res.send({
data: data,
status: 200
});
})
.catch(error => {
console.log(error);
res.status(500).send('Error occured');
});
});
作为响应,我得到如下响应:
{
"data": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
........
],
"status": 200
}
但是当我使用 pgAdmin 运行 相同的查询时,我得到的是国家名称而不是 'null'。有人可以帮我弄清楚这个问题吗?我是初学者,不知道这里出了什么问题。
提前致谢。
您的查询正在选择字段 name
,但您正在使用名为 json
的 属性 映射结果。您应该执行以下操作:
db.map(query, [], a => a.name)