我该如何解决:绑定消息提供3个参数,但是准备好的语句“”需要2个,redux中的错误?

时间:2018-12-31 06:15:54

标签: sql axios pg

我目前正在查询有效的距离。
我试图为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);
});
});

1 个答案:

答案 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;