1

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.

5
  • using this link and follow the steps in the answer stackoverflow.com/questions/40141332/… Commented Jan 30, 2020 at 7:51
  • @mohammadjavadahmadi Thanks for your prompt reply nut this is not working. Please help with another solution. Commented Jan 31, 2020 at 4:08
  • how do you get dbCon object? Is it coming from require('mysql2').createConnection() or require('mysql2/promise').createConnection()? Commented Feb 4, 2020 at 3:14
  • @AndreySidorov i have updated my question. please have a look of it. thanks in advance Commented Feb 4, 2020 at 3: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 github.com/sidorares/node-mysql2/issues/677 and github.com/sidorares/node-mysql2/pull/822 Commented Feb 4, 2020 at 9:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.