[英]JavaScript/ Node.js + Express - Function returning undefined before Complete
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.
基本上,它会查询我的文件系统并返回最后创建的GIF文件。 If there is no GIF, it looks for a PPM (another file type) and generates a GIF and returns that.
如果没有GIF,它将查找PPM(另一种文件类型)并生成一个GIF并将其返回。 If there's no PPM, it'll look into the MySQL database and generate a GIF based on what's in there.
如果没有PPM,它将检查MySQL数据库并根据其中的内容生成GIF。 All of this works fine.
所有这些都很好。
This module is called with a HTTP request in Express, where currentImg() is calling the module: 通过Express中的HTTP请求调用此模块,其中currentImg()正在调用该模块:
//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.
我已经尝试了一些调试,模块在完成来自module.exports函数中'queryDatabase(returnFile)'行的返回之前,返回了'undefined'。
// 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. 谢谢。
Use async waterfall to run function in series one after another. 使用异步瀑布依次运行一系列功能。 https://github.com/caolan/async#waterfall .
https://github.com/caolan/async#waterfall 。 So something like this.
像这样
async.waterfall([
function (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(currentImage)
});
},
function(currentImage, callback) {
// arg1 now equals 'one' and arg2 now equals 'two'
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;
}
}
})
}
callback(null,currentImage) })
}
], function (err, currentImage) {
console.log(currentImage);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.