编辑:我使用节点V8.0.0异步/等待节点的Postgres查询中foreach循环
我刚开始学习如何访问SQL数据库与节点的Postgres,和我有一点点无法访问多个数据库以可工作的格式收集数据,尤其是在forEach循环内执行多个查询时。试了几次后,我想异步/ AWAIT,但我得到了以下错误:
await client.connect()
^^^^^^
SyntaxError: Unexpected identifier
当我试图使用游泳池或调用顺序.query,我会相处的
东西线1
[]
could not connect to postgres Error: Connection terminated
这里是我的代码的缩写版本:
const { Client } = require('pg');
const moment = require('moment');
const _ = require('lodash');
const turf = require('@turf/turf');
const connString = // connection string
var collected = []
const CID = 300
const snaptimes = // array of times
var counter=0;
const client = new Client(connString);
function createArray(i,j) {
// return array of i arrays of length j
}
await client.connect()
snaptimes.forEach(function(snaptime){
var info = {}; // an object of objects
// get information at given snaptime from database 1
const query1 = // parametrized query selecting two columns from database 1
const result1 = await client.query(query1, [CID,snaptime]);
var x = result1.rows;
for (var i = 0; i < x.length; i++) {
// store data from database 1 into info
// each row is an object with two fields
}
// line up subjects on the hole
const query2 = // parametrized query grabbing JSON string from database 2
const result2 = await client.query(query2, [CID,snaptime]);
const raw = result2.rows[0].JSON_col;
const line = createArray(19,0); // an array of 19 empty arrays
for (var i = 0; i < raw.length; i++) {
// parse JSON object and record data into line
}
// begin to collect data
var n = 0;
var g = 0;
// walk down the line
for (var i = 18; i > 0; i--) {
// if no subjects are found at spot i, do nothing, except maybe update g
if ((line[i] === undefined || line[i].length == 0) && g == 0){
g = i;
} else if (line[i] !== undefined && line[i].length != 0) {
// collect data for each subject if subjects are found
line[i].forEach(function(subject){
const query 3 = // parametrized query grabbing data for each subject
const result3 = await client.query(query3,[CID,subject,snaptime]);
x = result3.rows;
const y = moment(x[0].end_time).diff(moment(snaptime),'minutes');
var yhat = 0;
// the summation over info depends on g
if (g===0){
for (var j = i; j <= 18; j++){
yhat = moment.duration(info[j].field1).add(yhat,'m').asMinutes();
}
} else {
for (var j = i; j <= 18; j++){
if (i<j && j<g+1) {
yhat = moment.duration(info[j].field2).add(yhat,'m').asMinutes();
} else {
yhat = moment.duration(info[j].field1).add(yhat,'m').asMinutes();
}
}
}
collected.push([y,yhat,n,i]);
});
}
n+=line[i].length;
g=0;
}
// really rough work-around I once used for printing results after a forEach of queries
counter++;
if (counter===snaptimes.length){
console.log(counter);
console.log(collected);
client.end();
}
});
在任何函数调用await之前进行异步解决问题非常好。但是,当我尝试向第一个forEach被调用的snaptime数组添加更多的快照时,我要么'错误:连接意外终止'或'错误:读ECONNRESET'。从打印语句开始,这些错误立即发生在query1上。将client.end()移动到forEach循环之后只会导致代码不运行 –
@HansyPiou当你使用async/await时,我建议使用'for(让snaptime快照时间){...}'而不是'snaptimes.forEach(function(snaptime){...});'因为这样你不会调用其他函数,并且更容易同步迭代步骤。 – rsp
现在整个事情都很棒!使用for()循环意味着我不得不将它们全部包装到一个更大的(异步)函数中,但是最终清除了node-postgres的问题,因为我可以在收集数据之前连接客户端,并在完成时关闭它。谢谢你的帮助! –