使用fs模块查找文件时遇到问题

时间:2013-03-08 03:51:17

标签: node.js fs

这是我的代码:

connection.query("SELECT * FROM images", function(err, rows, fields) {
            if (err) {
                console.log("Error: ");
                console.log(err);
            }
            else {
                console.log("rows: ");
                console.log(rows);

                for (var i = 0; i < rows.length; i++) {
                    var thisRow = rows[i];
                    var thisImageName = thisRow["imagename"];
                    var newDir = __dirname.split(path.sep).join("/");
                    var thisImagePath = newDir + "/public/uploads/" + thisImageName + ".jpg";
                    console.log(thisImagePath);
                    fs.exists(thisImagePath, function(exists) {
                        if (exists) {
                            console.log("true");
                            res.end();
                        }
                        else {
                            console.log(thisImageName + " does not exist."); res.end();
//Always returns false
                        }
                    });
                }
            }
        });

然而,

在控制台中:

fs.exists(/* imagepath */, function(exists){console.log(exists)}) //returns true

正如您所看到的,我使用node-mysql库来返回图像的文件名。我使用__dirname全局连接它们,然后我反转目录字符串中的斜杠以在.exists()调用中使用。当我使用fs模块检查图像文件是否存在时,它返回false。

然而,如果我在控制台中使用相同路径的fs.exists(),则返回true。有谁知道这里发生了什么?

0 个答案:

没有答案