Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 博士后&x2B;NodeJS插入,但不';不要结束脚本_Node.js_Postgresql_Node Postgres - Fatal编程技术网

Node.js 博士后&x2B;NodeJS插入,但不';不要结束脚本

Node.js 博士后&x2B;NodeJS插入,但不';不要结束脚本,node.js,postgresql,node-postgres,Node.js,Postgresql,Node Postgres,我有一个脚本,它使用soapxml,使之成为JSON,并使用它在postgresqldb for grafana上加载数据 在我的主脚本中,我这样做 ... do stuff const req = http.request(conn, (res) => { let data=''; res.on('data', (chun

我有一个脚本,它使用soapxml,使之成为JSON,并使用它在postgresqldb for grafana上加载数据

在我的主脚本中,我这样做

... do stuff

const req = http.request(conn, (res) =>
                         {
                             let data='';
                             res.on('data', (chunk) =>
                                    {
                                        data += chunk;
                                    });
                             res.on('end', () =>
                                    {
                                        var dados=processaDados.processaDados(processedXML);
                                        cargaBanco.gravaDados(dados);  
                                    });
                         }
                        );
... do stuff

req.end();

我的问题出在
cargaBanco.gravaDados(护墙板)

dados
是我传递给函数的JSON对象,该函数将在PostgreSQL数据库上加载JSON数据。我正在使用
节点postgres
DB

下面的代码来自
cargaBanco.gravaDados


var pg = require('pg');
var config = require('./config');

module.exports = {
  gravaDados: dados => {
    var agora = new Date();

    var totalDados = dados.length;

    var connection = new pg.Pool({
      connectionString: config.conn_string,
      max: 1,
      idleTimeoutMills: 5000,
      connectionTimeoutMills: 2000
    });

    if (typeof connection === 'unindentified') console.err('ERRO!');

    const parameterQuery =
      'INSERT INTO taskstatus VALUES(Default, CAST($1 as timestamp), $2, $3, $4, $5, $6, $7) returning *';

    connection.connect(async (err, client, done) =>
      // connection.connect( (err, client, done) =>
      {
        if (err) {
          console.log(err.stack);
        }
        for (linha of dados) {
          const dadosInserir = [
            agora,
            linha['Resource_Name'][0],
            linha['System'][0],
            linha['Resource_Type'][0],
            linha['Compound_Status'][0],
            linha['Observed_Status'][0],
            linha['Desired_Status'][0]
          ];

          await client.query(parameterQuery, dadosInserir, async (err, results) =>
            // client.query(parameterQuery,dadosInserir, (err, results)) =>
            {
              if (err) {
                console.log(err.stack);
              }
              // await client.release;
            }
          );
          // await client.release;
          // done();
        }
      }
    );
    connection.end();
  }
};

运行此脚本会导致数据加载到数据库上,但脚本会锁定,不会返回shell,甚至不会超时

我还尝试使用了
等待客户端。在。。。但没什么

有什么帮助吗?我做错什么了吗


很抱歉只包含代码片段,但这是内部工作,所以我需要以某种方式取消特征化…

pg promise
是一个更好的库,如果您不熟悉回调,可以使用nodejs处理Postgres的承诺。

我认为node Postgres不会返回承诺,您需要使用回调。@Vishnudev删除了代码中所有的async/wait,并没有什么。。。同样的效果…如果您不熟悉callbacks,请使用
pg promise
,它可以正常工作
pg promise
,谢谢