I'm using MySQL module in nodejs in order to read a particular column in each row to see if it is null, send another column on the same row to a web service to process the value to fill the null value.
here is my code:
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '123456789',
database : 'bank114'
});
var total_rows;
connection.connect();
connection.query('SELECT count(*) from bank114_x;', function (error, results, fileds) {
total_rows = results[0]['count(*)'];
console.log(total_rows);
iterator();
});
function iterator(){
for (var i = 1; i <= total_rows; i++) {
fetch_data(i)
}
}
function fetch_data(id){
connection.query({
sql: 'SELECT * FROM `bank114_x` WHERE `id` = ?',
timeout: 40000, // 40s
},
[id],
function (error, results, fields) {
console.error(error);
if (results[0].f6781 == null){
//send f6780 column to a webservice to calculate f6781
}
}
);
}
connection.end();
The problem is, I can do this on a single row of the table ( results[0]
returns the row), but when I use a for loop to iterate over all rows in the table, I get the error:
{ Error: Cannot enqueue Query after invoking quit.
at Protocol._validateEnqueue (/mnt/sdb2/WebstormProjects/translator/node_modules/mysql/lib/protocol/Protocol.js:204:16)
at Protocol._enqueue (/mnt/sdb2/WebstormProjects/translator/node_modules/mysql/lib/protocol/Protocol.js:139:13)
at Connection.query (/mnt/sdb2/WebstormProjects/translator/node_modules/mysql/lib/Connection.js:208:25)
at fetch_data (/mnt/sdb2/WebstormProjects/translator/main.js:29:16)
at iterator (/mnt/sdb2/WebstormProjects/translator/main.js:23:9)
at Query._callback (/mnt/sdb2/WebstormProjects/translator/main.js:18:5)
at Query.Sequence.end (/mnt/sdb2/WebstormProjects/translator/node_modules/mysql/lib/protocol/sequences/Sequence.js:88:24)
at Query._handleFinalResultPacket (/mnt/sdb2/WebstormProjects/translator/node_modules/mysql/lib/protocol/sequences/Query.js:139:8)
at Query.EofPacket (/mnt/sdb2/WebstormProjects/translator/node_modules/mysql/lib/protocol/sequences/Query.js:123:8)
at Protocol._parsePacket (/mnt/sdb2/WebstormProjects/translator/node_modules/mysql/lib/protocol/Protocol.js:279:23) code: 'PROTOCOL_ENQUEUE_AFTER_QUIT', fatal: false }
/mnt/sdb2/WebstormProjects/translator/main.js:36
if (results[0].f6780 == null){
^
TypeError: Cannot read property '0' of undefined
at Query._callback (/mnt/sdb2/WebstormProjects/translator/main.js:36:24)
at Query.Sequence.end (/mnt/sdb2/WebstormProjects/translator/node_modules/mysql/lib/protocol/sequences/Sequence.js:88:24)
at /mnt/sdb2/WebstormProjects/translator/node_modules/mysql/lib/protocol/Protocol.js:225:14
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
o do something with the asynchronous flow of javascript. How can I fix it?
It turns out that because of asynchronous flow in javascript, I reach to connection.end();
before fetching data from the database. so here is a workaround which fixed my problem:
function fetch_data(id){
connection.query({
sql: 'SELECT * FROM `bank114_x` WHERE `id` = ?',
timeout: 40000, // 40s
},
[id],
function (error, results, fields) {
if (results[0].f6781 == null){
//send f6780 column to a webservice to calculate f6781
}
}
);
if (id == total_rows)
{
connection.end();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.