So I'm having a lot of trouble getting my module to return the value I want it to. Basically it looks into my filesystem and returns the last created GIF file. If there is no GIF, it looks for a PPM (another file type) and generates a GIF and returns that. If there's no PPM, it'll look into the MySQL database and generate a GIF based on what's in there. All of this works fine.
This module is called with a HTTP request in Express, where currentImg() is calling the module:
//REQUEST CURRENT IMAGE
app.get('/img', function(req, res) {
console.log(currentImg());
});
This request always returns 'undefined'. I've tried a bit of debugging and the module is returning 'undefined' before it finishes getting the return from the 'queryDatabase(returnFile)' line in the module.exports function.
// CurrentImg
module.exports = function() {
imageFile = "";
queryDatabase(returnFile);
return imageFile;
}
function queryDatabase(callback) {
imageFile = "";
client.query("SHOW TABLES FROM mathsDB", function(err, result) {
if (err)
console.log(err);
else
currentImage = result[result.length - 1]["Tables_in_mathsDB"];
currentImage = currentImage.substring(5);
callback;
});
}
function returnFile() {
fs.stat("./img/image" + currentImage + ".gif", function(err, stat) {
if (err==null) {
imageFile = "/img/image" + currentImage + ".gif";
return imageFile;
}
else {
fs.stat("./img/image" + currentImage + ".ppm", function(err, stat) {
if (err==null) {
convert.convert("image" + currentImage);
imageFile = "/img/image" + currentImage + ".gif";
return imageFile;
}
else {
exportPPM.make();
setTimeout(waitConvert, 500);
function waitConvert() {
convert.convert("image" + currentImage);
imageFile = "/img/image" + currentImage + ".gif";
return imageFile;
}
}
});
}
});
}
I've tried doing some reading into callbacks, as you can see in my code, however that didn't seem to solve anything. I've read a bit in to promises too but they don't seem to provide a solid solution either.
Can anyone provide me with a pointer in the right direction to get this running in a non-blocking way?
Thanks.
callback;doesn't actually do anything. You need to call it, like any other function.