[英]Passing results of queries in promises
I am using node-postgres for a database connection.我正在使用 node-postgres 进行数据库连接。 For that I have APIs like that:为此,我有这样的 API:
app.post('/api/insert', urlEncodedParser, function(req, res) {
// do stuff
})
Now I need synchron steps:现在我需要同步步骤:
So I have following code:所以我有以下代码:
var userid = 0;
var listEntry = 0;
client
.query('SELECT "ID" FROM "User" WHERE "PW" = $1 AND "Name" = $2', [req.body.pw, req.body.name])
.then(res => userid = res.rows[0])
.catch(e => console.error(e.stack))
.query('SELECT " FROM "List" WHERE "UserID" = $1', [userid])
.then(res => listEntry = res.rows[0])
.catch(e => console.error(e.stack))
.query('INSERT INTO "List" ("UserID", "LastDate")', [userid, req.body.date])
.then(res => userid = res.rows[0])
.catch(e => console.error(e.stack))
.query('UPDATE "List" SET "LastDate" = $1', [req.body.date])
.then(res => userid = res.rows[0])
.catch(e => console.error(e.stack))
.finally(() { client.end()
});
What do I need to change that I can have this synchron steps as listed above?我需要改变什么才能拥有上面列出的同步步骤? Sorry, I am new to javascript and node-postgres.抱歉,我是 javascript 和 node-postgres 的新手。
Whatever you return in one then ends up in the following then.无论您在一个中返回什么,然后都会在下面结束。 For example:例如:
client.query("SELECT...")
.then((result) => {
// do something with the result
return 42;
})
.then((result) => console.log(result))
.then((result) => console.log(result))
The first log prints 42 because the previous then
returned 42 and the second prints undefined
because the previous then
returned nothing = undefined
.第一个日志打印 42 ,因为前一个then
42 ,第二个打印undefined
因为前一个then
nothing = undefined
。 So you actually want something like this:所以你实际上想要这样的东西:
client.query('SELECT "ID" FROM "User" WHERE "PW" = $1 AND "Name" = $2', [req.body.pw, req.body.name])
.then((res) => {
const userid = res.rows[0];
return client.query('SELECT " FROM "List" WHERE "UserID" = $1', [userid]).then(() => {
return client.query('INSERT INTO "List" ("UserID", "LastDate")', [userid, req.body.date]);
});
})
.then(() => {
return client.query('UPDATE "List" SET "LastDate" = $1', [req.body.date])
})
.catch(error => console.error(error))
.finally(() => client.end());
This was you don't even need the semi-global variables userid
and listEntry
.这是您甚至不需要半全局变量userid
和listEntry
。 This is generally a good thing.这通常是一件好事。
The code can be more simplified when using async/await .使用async/await时,代码可以更加简化。
If you don't want to execute the following queries because there are no rows in the first response you can do something like this:如果您不想执行以下查询,因为第一个响应中没有行,您可以执行以下操作:
client.query('SELECT "ID" FROM "User" WHERE "PW" = $1 AND "Name" = $2', [req.body.pw, req.body.name])
.then((res) => {
if (res.rows.length > 0) {
const userid = res.rows[0];
return client.query('SELECT " FROM "List" WHERE "UserID" = $1', [userid]).then(() => {
return client.query('INSERT INTO "List" ("UserID", "LastDate")', [userid, req.body.date]);
}).then(() => {
return client.query('UPDATE "List" SET "LastDate" = $1', [req.body.date])
})
}
})
.catch(error => console.error(error))
.finally(() => client.end());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.