0

My MySQL database has 2 million records in my one table. i am developing an api using node.js to fetch records from the table.

I am using mysql2 package to connect to the database. following is my code to fetch all records from table.

var query1 = dbCon.query('select * from Post');

query1
    .on('error', function (err) {
        // Handle error, an 'end' event will be emitted after this as well
        console.log(err)

    })
    .on('fields', function (fields) {
        // the field packets for the rows to follow
        console.log(fields);
    })
    .on('result', function (row) {
        // Pausing the connnection is useful if your processing involves I/O
        dbCon.pause();

        processRow(row, function () {
            console.log("row is ", row);
            dbCon.resume();
        });
    })
    .on('end', function (row) {
        console.log(row);
    });

database connection code

const mysql = require('mysql2');
const serverConfig = require('../global/serverConfig');
const conPool = mysql.createPool({
    host: serverConfig.dbHost,
    database: serverConfig.dbName,
    user: serverConfig.dbUser,
    password: serverConfig.dbPass,
    multipleStatements: true
    // timezone: 'IST'
})
conPool.on('connection', function (connection) {
   // console.log('Connected to MySql db');
});
conPool.on('error', ()=>{
   // console.log('Not Connected to MySql db');
})
module.exports = conPool.promise();

When i hit the API i am getting an error

Error: TypeError: query1.on is not a function

Please suggest me a solution or any alternative way to fetch and process such a huge data.

Rahul Lad
  • 337
  • 1
  • 5
  • 15
  • using this link and follow the steps in the answer https://stackoverflow.com/questions/40141332/node-js-mysql-error-handling – mohammad javad ahmadi Jan 30 '20 at 07:51
  • @mohammadjavadahmadi Thanks for your prompt reply nut this is not working. Please help with another solution. – Rahul Lad Jan 31 '20 at 04:08
  • how do you get `dbCon` object? Is it coming from `require('mysql2').createConnection()` or `require('mysql2/promise').createConnection()`? – Andrey Sidorov Feb 04 '20 at 03:14
  • @AndreySidorov i have updated my question. please have a look of it. thanks in advance – Rahul Lad Feb 04 '20 at 03:49
  • currently promise connection ( as well as promisePool.query() result ) don't support row streaming. Your best bet right now is non-promise callback api. See https://github.com/sidorares/node-mysql2/issues/677 and https://github.com/sidorares/node-mysql2/pull/822 – Andrey Sidorov Feb 04 '20 at 09:41

0 Answers0