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.