Я пытаюсь загрузить файл csv в postgres с помощью pg-copy-stream, но получаю сообщение об ошибке

Я пытаюсь загрузить CSV-файл в postgres с помощью pg-copy-stream, но получаю сообщение об ошибке, вызванное выпуском на клиенте, который уже был отправлен в пул. Пожалуйста, помогите. Я использую для этого модуль узла pg-copy-stream. Я сохранил файл на серверном файловом сервере с помощью multer.

ОШИБКА:

Error: Release called on client which has already been released to the pool.
    at throwOnDoubleRelease (C:\Users\MRINDAS\Desktop\file_upload\node_modules\pg-pool\index.js:27:9)
    at CopyStreamQuery.<anonymous> (C:\Users\MRINDAS\Desktop\file_upload\node_modules\pg-pool\index.js:287:9)
    at CopyStreamQuery.emit (events.js:203:15)
    at CopyStreamQuery.handleError (C:\Users\MRINDAS\Desktop\file_upload\node_modules\pg-copy-streams\copy-from.js:79:10)
    at process.nextTick (C:\Users\MRINDAS\Desktop\file_upload\node_modules\pg\lib\client.js:74:13)
    at process._tickCallback (internal/process/next_tick.js:61:11)
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const multer = require('multer');
var fs = require('fs');
const { Pool } = require('pg');
var copyFrom = require('pg-copy-streams').from;
const path = require('path');
const app = express();

const pool = new Pool({
  user: 'postgres',
  host: 'localhost',
  database: 'api',
  password: 'root',
  port: 5432,
})

app.use(cors({ origin: "*" }));
app.use(bodyParser.json());
app.listen(3000, () => {
  console.log("The server started on port 3000");
});

const storage = multer.diskStorage({
  destination: (req, file, callBack) => {
    callBack(null, 'uploads')
  },
  filename: (req, file, callBack) => {
    callBack(null, `${file.originalname}`)
  }
})

const upload = multer({ storage: storage })

//let upload = multer({ dest: 'uploads/' })

app.get("/", (req, res) => {
  res.send(
    `<h1 style='text-align: center'>
                Wellcome to FileTesting 
                <br><br>
                <b style="font-size: 182px;"></b>
            </h1>`
  );
});

app.post('/file', upload.single('file'), (req, res, next) => {
  file = req.file;
  var x = path.join(__dirname + req.file.destination, req.file.filename);
  console.log(x);
  if (!file) {
    const error = new Error('No File')
    error.httpStatusCode = 400
    return next(error)
  }

  pool.connect(function (err, client, done) {
    var stream = client.query(copyFrom('COPY sales FROM STDIN'))
    var fileStream = fs.createReadStream(x)
    fileStream.on('error', done)
    stream.on('error', done)
    stream.on('finish', done)
    fileStream.pipe(stream)
  })

  res.status(200);
})

1 ответ

I tried this and it worked instead of CSV i am using tab delimited. because in CSV if we have comma between the columns then it takes it as separate columns




    const express = require("express");
    const cors = require("cors");
    const bodyParser = require("body-parser");
    const multer = require('multer');
    var fs = require('fs');
    const { Pool, Client} = require('pg')
    var copyFrom = require('pg-copy-streams').from;
    const path = require('path');
    const app = express();

    const client = new Client({
      user: 'postgres',
      host: 'localhost',
      database: 'api',
      password: 'root',
      port: 5432,
    })

 client.connect()

    app.use(cors({ origin: "*" }));
    app.use(bodyParser.json());
    app.listen(3000, () => {
        console.log("The server started on port 3000");
    });

    const storage = multer.diskStorage({
        destination: (req, file, callBack) => {
            callBack(null, 'uploads')
        },
        filename: (req, file, callBack) => {
            callBack(null, `${file.originalname}`)
        }
      })

    const upload = multer({ storage: storage })

    //let upload = multer({ dest: 'uploads/' })

    app.get("/", (req, res) => {
        res.send(
          `<h1 style='text-align: center'>
                Wellcome to FileTesting 
                <br><br>
                <b style="font-size: 182px;"></b>
            </h1>`
        );
      });

      app.post('/file', upload.single('file'), (req, res, next) => {
         file = req.file;
        //  var filename = __dirname+req.file.destination+req.file.filename;
        //  console.log(filename);
        if (!file) {
          const error = new Error('No File')
          error.httpStatusCode = 400
          return next(error)
        }


    // Execute Copy Function
    var stream = client.query(copyFrom(('COPY sales FROM STDIN')))
    var fileStream = fs.createReadStream(path.join(__dirname,req.file.destination,req.file.filename ))

    fileStream.on('error', (error) =>{
      console.log(`Error in reading file: ${error}`)
    })
    stream.on('error', (error) => {
      console.log(`Error in copy command: ${error}`)
    })
    stream.on('end', () => {
      console.log(`Completed loading data into ${targetTable}`)
      client.end()
    })
    fileStream.pipe(stream);


      res.status(200); 
      })
Другие вопросы по тегам