FS Module. fs.rename is giving Eonent -2. Seems like a problem with the paths

Viewed 16

I have an error with the filesystem(fs) module. I am building paths to files dynamically and renaming them, however i am getting the following error:

[Error: ENOENT: no such file or directory, rename '/home/dylan/covers/oehf12GC2t.png' -> '/home/dylan/covers/Hello.png'] {
0|app  |   errno: -2,
0|app  |   code: 'ENOENT',
0|app  |   syscall: 'rename',
0|app  |   path: '/home/dylan/covers/oehf12GC2t.png',
0|app  |   dest: '/home/dylan/covers/Hello.png'
0|app  | }

Here is my code for reference:

app.post('/edit/:id', jsonParser, function(req, res) {
  console.log(req.params.id)
  console.log(JSON.stringify(req.body.newid))
  req.body.newid = req.body.newid.replace(/"/g, "")
  console.log((req.body.newid))
  
  fs.rename(__dirname + "/files/"+req.params.id, __dirname + "/files/"+req.body.newid.replace(/"/g, ""), (err) => {
  if (err) console.log(err)
   console.log("File Renamed")
  // List all the filenames after renaming
});

fs.rename(__dirname + "/covers/"+req.params.id.replace("pdf","png"), __dirname + "/covers/"+req.body.newid.replace("pdf","png"), (err) => {
  if (err) console.log(err)
   res.send("file renamed")
  // List all the filenames after renaming
});
})
0 Answers
Related