我目前正在查询有效的距离。
我试图为type
添加另一个参数,以按两个参数排序。
当我添加type
参数时,我收到错误消息:
bind消息提供3个参数,但准备好的语句“”要求 2
router.get('/:lat/:lon/:type', (req, res) => {
console.log('location GET local route');
let queryText = `SELECT *, distance($1, $2, location.latitude, location.longitude) as distance FROM location ORDER BY distance, type;`
pool.query(queryText, [req.params.lat, req.params.lon, req.params.type])
.then((result) => {
res.send(result.rows);
// console.log(result.rows)
}).catch((error) => {
console.log(error);
res.sendStatus(500);
});
});
答案 0 :(得分:0)
您的查询未使用第三个参数,这就是您收到此错误的原因:
SELECT *, distance($1, $2, location.latitude, location.longitude) as distance FROM location ORDER BY distance, type;
您可以通过删除第三个参数来解决此问题:
pool.query(queryText, [req.params.lat, req.params.lon, req.params.type])
收件人:
pool.query(queryText, [req.params.lat, req.params.lon])
或者如果您想在order by子句中使用第三个参数,则需要将type
替换为$3
:
SELECT *, distance($1, $2, location.latitude, location.longitude) as distance FROM location ORDER BY distance, $3;
更新
如果您只想按类型而不是距离进行排序,则将其从查询中删除:
SELECT *, distance($1, $2, location.latitude, location.longitude) as distance FROM location ORDER BY $3;