Reputation: 305
I have a check script with a MySQL query, the problem is that the authorization function is executed before it was done MySQL query so it returns always false. How can fix it?
mysql= require('mysql');
var connection = mysql.createConnection({});
check_auth(req.query.s){
var sql = "query to mysql";
connection.query(sql, function(err, results) {
if (err) throw err;
if (results.length > 0) {
return true;
}else{
return false;
}
}
io.configure(function () {
io.set('authorization', function(req, callback) {
var auth=check_auth(req.query.s);
console.log(auth);
if (auth===true){
return callback(null, true);
}else{
return callback('notauth',false);
}
});
});
EDIT: new structure as Jarema but still don't work
mysql= require('mysql');
var connection = mysql.createConnection({});
function check_auth(input, callback){
var sql = "query to mysql";
connection.query(sql, function(err, results) {
if (err) callback(err);
if (results.length > 0) {
callback(null,results.values);
}else{
callback(null, false);
}
}
io.configure(function () {
io.set('authorization', function(req, callback) {
check_auth(req.query.s, function(err, result) {
if (err) {
//handle that error somehow. we dont not structure of Your func.
return console.log('error:(');
}
if(result === false) {
return callback('notauth', false);
} else {
return callback(null, result);;
}
});
});
});
error is
TypeError: undefined is not a function
at Query._callback (myserver.js:54:12)
at Query.Sequence.end (node_modules/mysql/lib/protocol/sequences/Sequence.js:78:24)
at Query._handleFinalResultPacket (node_modules/mysql/lib/protocol/sequences/Query.js:143:8)
at Query.EofPacket (node_modules/mysql/lib/protocol/sequences/Query.js:127:8)
at Protocol._parsePacket (node_modules/mysql/lib/protocol/Protocol.js:205:24)
at Parser.write (node_modules/mysql/lib/protocol/Parser.js:62:12)
at Protocol.write (node_modules/mysql/lib/protocol/Protocol.js:37:16)
at Socket.<anonymous> (node_modules/mysql/lib/Connection.js:73:28)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:746:14)
line 54 is
callback(null,results.values);
Upvotes: 1
Views: 1546
Reputation: 3586
If check_auth query is still processing when next function/if statment is called, for me it means it's asynchronous. If it was synchronous, it would block code execution until its finished.
EDIT: As Jonatham mentioned, the query function itself is probably async. If You put it in 'normal' synchronous function, it doenst mean You can return inner async result to parent function, and from parent return it again. You have to deal with callbacks.
Here is PROBABLY working example, that for sure need adjusting, as we dont know what's inside auth function:
mysql= require('mysql');
var connection = mysql.createConnection({});
function check_auth(input, callback){
var sql = "query to mysql";
connection.query(sql, function(err, results) {
if (err) callback(err);
if (results.length > 0) {
callback(null, true);
}else{
callback(null, false);
}
}
io.configure(function () {
io.set('authorization', function(req, callback) {
check_auth(req.query.s, function(err, result) {
if (err) {
//handle that error somehow. we dont not structure of Your func.
return console.log('error:(');
}
if(result === true) {
return callback(null, true);
} else {
return callback('notauth', false);
}
});
});
});
Upvotes: 1