gpt4 book ai didi

javascript - 错误: Connection terminated - ASYNC nodeJS

转载 作者:行者123 更新时间:2023-12-02 21:27:14 25 4
gpt4 key购买 nike

当所有数据输入正确时如何结束我的异步连接?

即使在循环外声明连接结束,结构也会在第一次 INSERT 之后最终确定

代码

require('es6-promise').polyfill();
require('isomorphic-fetch');
let queryAPI = {
"query": `{
squads {
name
cards(includedOnKanban: true, closed: false, archived: false, cancelled: false, swimlaneName:\"Atividades Nao Transacionais\", updatedSince: \"2020-01-01T00:00:00-0300\") {
identifier
title
description
status
priority
assignees {
fullname
email
}
swimlane
workstate
}
}
}`
};
(async () => {
const rawResponse = await fetch('https://www.bluesight.io/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Bluesight-API-Token': 'token-here'
},
body: JSON.stringify(queryAPI)
});
const content = await rawResponse.json();
const { Client } = require('pg');
const client = new Client({
user: 'postgres',
host: '127.0.0.1',
database: 'postgres',
password: 'postgres',
port: 5432
})
client.connect();

const query = `INSERT INTO tb_bluesight
(identifier,title,description,status,priority,date_insert)
VALUES ($1, $2, $3, $4, $5, current_timestamp)`;
var data = Object.keys(content);
var squads = Object.keys(content[data]);
var cards = Object.keys(content[data][squads][0]['cards']);

try{
for(x in cards){
const parameters = [
content[data]["squads"][0]["cards"][x]['identifier'],
content[data]["squads"][0]["cards"][x]['title'],
content[data]["squads"][0]["cards"][x]['description'],
content[data]["squads"][0]["cards"][x]['status'],
content[data]["squads"][0]["cards"][x]['priority']

];
client.query(query, parameters, (err, res) => {

console.log(err, res);

})
}
}catch(e){
console.log("undefined");

}

client.end();

})();

输出

Error: Connection terminated at Connection. (C:\Users\TESTE\Documents\Autoportal\api\node_modules\pg\lib\client.js:254:9) at Object.onceWrapper (events.js:417:28) at Connection.emit (events.js:323:22)

最佳答案

您可以尝试使用await client.connect()

并且,对于您的疑问,这个

    res = await client.query(query, parameters);

代替这个

    client.query(query, parameters, (err, res) => {
console.log(err, res);
})

关于javascript - 错误: Connection terminated - ASYNC nodeJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60710001/

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