I'm currently getting data from my database by the way of PHP using Nodejs. The problem is that the script that I want to execute after grabbing datas is actually executing directly after the instance of my grabing function and I get everytime an error saying that my datas are undefined.
Here is my code :
var http = require('http'),
redis = require("redis");
client = redis.createClient();
client.on("error", function (err) {
console.log("Error " + err);
});
var sessionData;
// Check if we have session create with sessionID gived before (hash)
client.get('sessions:'+sessionId, function (err, result) {
var resData;
//Check if we have session with the session id
// If we have not execute function to get the data
if(err || !result)
{
executefunction(get_sess_data,sessionId);
}
// If we have, get it from redis
else
{
resData = result;
console.log(resData);
}
// settings session data with the result
sessionData = JSON.parse(resData);
});
// Execute function with sessionid as value
function executefunction(someFunction, value) {
someFunction(value);
}
// Getting data from database (using script from Codeigniter)
var get_sess_data = function(response)
{
var options = {
host: 'www.example.dev',
port: 80,
path: '/index.php/user_session/decrypt?id='+sessionId
};
http.get(options, function(result) {
result.on("data", function(chunk) {
client.setex('sessions:'+sessionId, 21600, chunk.toString());
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
result.end(response);
});
};
FYI, I know the code to grab data is working because I can return them if I don't execute the following script "sessionData = JSON.parse(resData);" even if I place it in my if statement.
I'm stuck for hours on it. If anyone have an issue, let me know.