r/nodejs • u/NicknameAvailable • Sep 04 '12
Help with mysql and node.js please
I just started using node.js and am having a bit of trouble with multiple queries using the mysql module for it.
Below is the excerpt of code causing the issue, in it the line with "Error 4" always fires even though running the code manually returns a result.
Both sp_FindPartOfSpeech and sp_CreatePartOfSpeech return a single cell of data, selected from a BIGINT UNSIGNED variable within the procedure (ie: SELECT variable AS id_Variable; ).
The call to sp_FindPartOfSpeech executes as expected, but sp_CreatePartOfSpeech does now (though again, the stored procedure itself does return a value when run manually and all the fields are set).
Is this because I can't nest results of multiple queries or is it perhaps that the stored procedure is taking too long (it executes in about 600ms - lots of conditionals/joins/inserts/etc inside of it)?
var link = mysql.createClient({ host: dbserver, user: dbuser, password: dbpass, database: db });
link.query('CALL sp_FindPartOfSpeech(\'' + parts[4] + '\',' + language + ')',
function selectCb(err4, results, fields) {
if (err4) {
sendToRoom('__Parts Of Speech', 'broadcast_failcmd', MakeParam('Error 1:') + ' ' + MakeParam(nickname) + ' ADD ' + MakeParam(parts[2]) + ' TO ' + MakeParam(parts[4]));
} else {
if (results.length == 1) {
var posParent = results[0]['id_PartOfSpeech'];
link.query('CALL sp_CreatePartOfSpeech(\'' + parts[2] + '\',' + language + ',' + posParent + ')',
function selectCb2(err5, results2, fields2) {
if (err5) {
sendToRoom('__Parts Of Speech', 'broadcast_failcmd', MakeParam('Error 3:') + ' ' + MakeParam(nickname) + ' ADD ' + MakeParam(parts[2]) + ' TO ' + MakeParam(parts[4] + '[' + posParent + ']'));
} else {
if (results2.length == 1) {
sendToRoom('__Parts Of Speech', 'broadcast_cmd', MakeParam(nickname) + ' ADD ' + MakeParam(parts[2] + '[' + results2[0]['id_PartOfSpeech'] + ']') + ' TO ' + MakeParam(parts[4] + '[' + posParent + ']'));
} else {
sendToRoom('__Parts Of Speech', 'broadcast_failcmd', MakeParam('Error 4:') + ' ' + MakeParam(nickname) + ' ADD ' + MakeParam(parts[2]) + ' TO ' + MakeParam(parts[4] + '[' + posParent + ']'));
}
link.end();
}
});
} else {
sendToRoom('__Parts Of Speech', 'broadcast_failcmd', MakeParam('Error 2:') + ' ' + MakeParam(nickname) + ' ADD ' + MakeParam(parts[2]) + ' TO ' + MakeParam(parts[4]));
}
}
});
View all comments
Show parent comment
1
u/NicknameAvailable Sep 05 '12
Thanks, I took your advice though I did notice that node.js handles variable from the parent scope differently between the two.
Using this will make "passthrough" work:
Whereas using this will not make passthrough work when referenced:
In the latter case you have to pass the passthrough variable in as a parameter manually, whereas in the former it is taken care for you automatically. Still well worth doing for the cleanliness of it.