gpt4 book ai didi

node.js - 在 pg-promise 中编写子查询的好方法

转载 作者:搜寻专家 更新时间:2023-11-01 00:48:25 24 4
gpt4 key购买 nike

Incase 我必须在一个 API 中执行更多查询,我必须以什么方法继续这是在主查询中编写子查询的更好方法吗不使用 db.task 或 db.tx 否则我必须使用这些方法。像下面这样

function userChangePassword(req, res) {
const changePwd = req.swagger.params.body.value;

const token = req.headers.authorization;
token.replace('Bearer ', '');
const decoded = jwt_decode(token);

const newpass = changePwd.newPassword;
const user_id = decoded.userId;

const userSel = `select * from xx."yy" where "userId" = '${user_id}' AND "status" = 1`;
const updatePwd = `update xx."yy" set "password" = '${newpass}' where "userId" = '${user_id}' `;

db.query(userSel).then((usrResult) => {
if (usrResult.length > 0) {
db.query(updatePwd).then(() => {
res.send(response.success("The password has been changed successfully", []));
});
} else {
res.send(response.success("The password has not changed successfully", []));
}
})
.catch((err) => {
if (util.isError(err)) res.error('NotFoundError', err); // return 404
else res.error('InternalServerError', err); // else 500
});
}

请帮助我解决这个困惑..谢谢。

最佳答案

如果查询之间存在依赖关系,则必须使用方法task/tx。否则,您可以使用 helpers.concat 将查询连接成一个查询。 ,并将它们作为一个查询执行,这样会更快。你可以使用方法 multi/multiResult ,如果您希望从多查询返回数据。

关于node.js - 在 pg-promise 中编写子查询的好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55625857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com