使用 node-postgres 创建依赖于先前查询结果的查询
Creating a query that relies on results of previous query using node-postgres
对 postgres 数据库的第一个查询是 SELECT
查询获取当天的所有 shift/break 数据,用于确定扫描类型
第二个查询是一个 INSERT
查询,它依赖于第一个查询的结果
我的处理函数现在看起来像这样:
const scanEvent = (request, response) => {
employee_id = request.body.employee_id;
var shifts;
Promise.all([
pool.query('Select * FROM shifts WHERE employee_id= AND date=CURRENT_DATE', [employee_id])
]).then(function([queryResults]) {
shifts = queryResults.rows;
}).catch(function(e) {
response.status(500).send('Error retrieving data');
})
// based on the results of the query there will be a bunch of different cases for
// INSERT queries to put in the proper data to the database
if(shifts.length == 0) {
Promise.all([
pool.query('INSERT INTO shifts (employee_id, start_time) VALUES (, NOW())', [employee_id])
]).then(function() {
response.status(204).send('Successfully Inserted');
}).catch(function (e) {
response.status(500).send("Error");
});
} // else if ... handle all other cases
}
我的问题是我无法访问第一个查询的结果,因为 shifts
变量似乎在第一个 Promise.all
范围内是局部的
** EDIT **
我现在意识到我的方法不是最优的(只是在学习 node-postgres)解决这个问题的更好方法是使用 async / await:
const scanEvent = async (request, response) => {
employee_id = request.body.employee_id;
var shifts;
const getShifts = await pool.query('Select * FROM shifts WHERE employee_id= AND date=CURRENT_DATE', [employee_id]);
shifts = getShifts.rows;
// based on the results of the query there will be a bunch of different cases for
// INSERT queries to put in the proper data to the database
if(shifts.length == 0) {
await pool.query('INSERT INTO shifts (employee_id, start_time) VALUES (, NOW())', [employee_id]);
} // else if ... handle all other cases
}
变量shifts
在执行if
语句时还没有值,因为它只在[=13]中接收它的值=] 功能。因此,如果后半部分或您的代码依赖于 shifts
的值,请将其移动到 .then
函数中:
.then(function([queryResults]) {
shifts = queryResults.rows;
if(/* first scan therefore scanning in for shift */) {
...
} // else if ... handle all other cases
})
(如果要并行执行两个独立的查询,请参见。)
对 postgres 数据库的第一个查询是 SELECT
查询获取当天的所有 shift/break 数据,用于确定扫描类型
第二个查询是一个 INSERT
查询,它依赖于第一个查询的结果
我的处理函数现在看起来像这样:
const scanEvent = (request, response) => {
employee_id = request.body.employee_id;
var shifts;
Promise.all([
pool.query('Select * FROM shifts WHERE employee_id= AND date=CURRENT_DATE', [employee_id])
]).then(function([queryResults]) {
shifts = queryResults.rows;
}).catch(function(e) {
response.status(500).send('Error retrieving data');
})
// based on the results of the query there will be a bunch of different cases for
// INSERT queries to put in the proper data to the database
if(shifts.length == 0) {
Promise.all([
pool.query('INSERT INTO shifts (employee_id, start_time) VALUES (, NOW())', [employee_id])
]).then(function() {
response.status(204).send('Successfully Inserted');
}).catch(function (e) {
response.status(500).send("Error");
});
} // else if ... handle all other cases
}
我的问题是我无法访问第一个查询的结果,因为 shifts
变量似乎在第一个 Promise.all
** EDIT **
我现在意识到我的方法不是最优的(只是在学习 node-postgres)解决这个问题的更好方法是使用 async / await:
const scanEvent = async (request, response) => {
employee_id = request.body.employee_id;
var shifts;
const getShifts = await pool.query('Select * FROM shifts WHERE employee_id= AND date=CURRENT_DATE', [employee_id]);
shifts = getShifts.rows;
// based on the results of the query there will be a bunch of different cases for
// INSERT queries to put in the proper data to the database
if(shifts.length == 0) {
await pool.query('INSERT INTO shifts (employee_id, start_time) VALUES (, NOW())', [employee_id]);
} // else if ... handle all other cases
}
变量shifts
在执行if
语句时还没有值,因为它只在[=13]中接收它的值=] 功能。因此,如果后半部分或您的代码依赖于 shifts
的值,请将其移动到 .then
函数中:
.then(function([queryResults]) {
shifts = queryResults.rows;
if(/* first scan therefore scanning in for shift */) {
...
} // else if ... handle all other cases
})
(如果要并行执行两个独立的查询,请参见