An NodeJs app we are wrting has a problem with the query data returned as an array being undefined
. I cannot find an exact match for our issue.
The following test case reproduces the problem. It is something to do with the calling function not waiting for the called function to finish? I’m not a JavaScript expert, and this behaviour does not make sense to me.
themod.js
module.exports = {
getData: () => {
let dbc = require('./mods/db.js');
dbc.query(`
SELECT 1 rn, 'One' rt UNION
SELECT 2 rn, 'Two' rt UNION
SELECT 3 rn, 'Three' rt ;
`,
function (err, rows) {
if (err) {
console.log ( ' Error 1: ' , err );
} else {
arows = module.exports.getSubData();
console.log ( 'arows.length: ', arows.length );
}
})
},
getSubData: () => {
let dbc = require('./mods/db.js');
dbc.query(`
SELECT 10 rn, 'Ten' rt UNION
SELECT 20 rn, 'Twenty' rt UNION
SELECT 30 rn, 'Thirty' rt ;
`,
function (err, rows) {
if (err) {
console.log ( ' Error 3: ' , err );
} else {
console.log ( 'arows: ', rows.length );
return( rows );
}
})
}
}
theapp.js:
let tm = require('./themod.js');
tm.getData();
When it’s run:
node theapp.js
/path/node/node_modules/mysql/lib/protocol/Parser.js:437
throw err; // Rethrow non-MySQL errors
^
TypeError: Cannot read property 'length' of undefined
at Query.<anonymous> (/path/node/themod.js:17:54)
at Query.<anonymous> (/path/node/node_modules/mysql/lib/Connection.js:526:10)
at Query._callback (/path/node/node_modules/mysql/lib/Connection.js:488:16)
at Query.Sequence.end (/path/node/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
at Query._handleFinalResultPacket (/path/node/node_modules/mysql/lib/protocol/sequences/Query.js:149:8)
at Query.EofPacket (/path/node/node_modules/mysql/lib/protocol/sequences/Query.js:133:8)
at Protocol._parsePacket (/path/node/node_modules/mysql/lib/protocol/Protocol.js:291:23)
at Parser._parsePacket (/path/node/node_modules/mysql/lib/protocol/Parser.js:433:10)
at Parser.write /path/node/node_modules/mysql/lib/protocol/Parser.js:43:10)
at Protocol.write (/path/node/node_modules/mysql/lib/protocol/Protocol.js:38:16)
As requested, mods/db.js:
var mysql = require('mysql');
var dbConn = mysql.createConnection({
host : 'localhost',
user : 'unam',
password : 'pwrd',
database : 'dbname'
});
dbConn.connect ( function(err) {
if (err) {
console.error( "DB Connect failed ", err);
}
});
module.exports = dbConn;
2
Answers
JavaScript works with synchronous and asynchronous calls.
Asynchronous calls are named Promises and use what we call callback
functions to execute code once the asynchronous sequences is done.
In your case, it seems the second argument of
is the callback function.
Can you provide definition of query function in ./mods/db.js please it will help to find out what is the problem
I think your problem come from the scope of your getSubData function.
You do not return anything, you return row in the callback function but nothing at the getSubData level.
That’s why arrow return null and then .length cannot be red.
You can try with this
you will then get the full query and the result inside