1
I’m running a few asynchronous functions, who receive a callback, this callback gets as parameter a Boolean(true or false). When executing I have the error callback is not defined.
[code]
file - app.js
session.checkip(req.ip, function(result) {
console.log(result);
});
file - Session.js
exports.checkip = function(ip, callback) {
var r = false;
let connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '***********',
database: 'website_personal_trainer'
});
connection.connect(function(err) {
if (err) {
return console.error('error: ' + err.message);
}
console.log('Connected to the MySQL server. checkip');
});
connection.query('select * from session', function(err, result, fields){
for (var i in result)
if (result[i].ip == ip) {
r = true;
break;
}
connection.end(function(err) {
if (err) {
return console.log('error:' + err.message);
}
console.log('Close the database connection.');
callback(r);
});
});
[Debuging] When changing the parameter to receive strings, the function performs normally. Can you help me?
[update] - 23/02/2019 The code presented works correctly. After having found an alternative solution for what I wanted to do, I tested this same code again and did not get any error. Therefore, the above error does not have to do with this code.
I don’t think that mistake is in the code you have in the question.
– Sergio
The function
callback
does not exist in the context in which it was called, within another callback.---> callback(r)
, in this case, you can correct by redeclaring the variable:var callback = callback
just belowexports.checkip = function(ip, callback) {
– edson alves
@edsonalves failed to notice. Session.checkip is not a callback. Could explain your reasoning pf.
– Pedro Correia
You receive a callback in this function and try to call it inside another callback. This other one can not see the received function in form
– edson alves