[英]Error: ENOENT: no such file or directory, unlink
As you can see, there is a file at the path.如您所见,路径中有一个文件。 But fs says no such file or directory.
但是 fs 说没有这样的文件或目录。 I can't understand why?
我不明白为什么?
In another file, I can remove with the same code.在另一个文件中,我可以使用相同的代码删除。
My boat.js file:我的boat.js 文件:
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)
Can you try this instead:你可以试试这个吗:
fs.unlink("public"+path,function(err){
if(err) throw err;
console.log('File deleted!');
});
You should first install the path
module via CLI:您应该首先通过 CLI 安装
path
模块:
npm install path --save
and use it:并使用它:
fs.unlink(path.join("public/" + path, photo.id + ".jpg"), function(response) {
// handle the callback
});
Check if the path variable you are adding to the "/public" has a "/" at the beginning.检查您添加到“/public”的路径变量是否以“/”开头。 If there is no/ seperating it it will treat the path as one.
如果没有/分隔它,它会将路径视为一个。
Install and import path
module.安装并导入
path
模块。 this helps you https://nodejs.org/api/path.html这可以帮助您https://nodejs.org/api/path.html
Instead of "../public"+path
use path.join("../", "public", path);
而不是
"../public"+path
使用path.join("../", "public", path);
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.
如果它位于在线的 Web 服务器上,则您需要指定它的完整路径。
on a " local machine " it might look something like this:在“本地机器”上,它可能看起来像这样:
fs.unlink('/home/user/project/someothername/'+filename, err => {
// handler
});
Consider like you are in app.js
file and give the relative path of deleting file to the fs.unlink('path/to/file')
.假设您在
app.js
文件中,并将删除文件的相对路径提供给fs.unlink('path/to/file')
。 it will work!它会工作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.