问题
As you can see, there is a file at the path. But fs says no such file or directory. I can't understand why?
In another file, I can remove with the same code.
My boat.js file:
boat.findById(req.params.id,function(err, foundBoat) {
if(err){
console.log(err);
}else{
foundBoat.boatsFoto.forEach(function(path){
console.log(typeof(path));
fs.unlink("../public"+path,function(err){
if(err) throw err;
console.log('File deleted!');
});
});
}
});
And it is my error:
Error: ENOENT: no such file or directory, unlink '../public/uploads/akingokay/BoatsFoto/1524411110335kiralik-tekne.jpg'
at Error (native)
And you can see my file system
回答1:
Can you try this instead:
fs.unlink("public"+path,function(err){
if(err) throw err;
console.log('File deleted!');
});
回答2:
You should first install the path
module via CLI:
npm install path --save
and use it:
fs.unlink(path.join("public/" + path, photo.id + ".jpg"), function(response) {
// handle the callback
});
回答3:
Check if the path variable you are adding to the "/public" has a "/" at the beginning. If there is no/ seperating it it will treat the path as one.
回答4:
Install and import path
module. this helps you https://nodejs.org/api/path.html
Instead of "../public"+path
use path.join("../", "public", path);
回答5:
It depends where you host the server. If it is on a local machine then you will probably need to specify the complete path to the file(s) that you want to delete or manipulate. If it is on a web server that is live then you will need to specify the complete path to it.
on a "local machine" it might look something like this:
fs.unlink('/home/user/project/someothername/'+filename, err => {
// handler
});
来源:https://stackoverflow.com/questions/49968094/error-enoent-no-such-file-or-directory-unlink