0

Here is my code:

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
                        }
                    });
                }
            }
        });

HOWEVER,

In the console:

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

As you can see I'm using the node-mysql library to return the file names of my images. I concatenate them using the __dirname global and then I reverse the slashes in the directory string to use in the .exists() call. When I use the fs module to check if the image file exists, it returns false.

HOWEVER, if I use fs.exists() with the same path in the console, it returns true. Does anyone know what's going on here?

4

0 回答 0