0

im using the fs.rename from node, however im running into the issue that im not able to return the new fileName after it has been renamed.

fs.rename(file, directoryPath + md5(buf + randomNumber()) + '.mp3',(err)=> {
  if (err) return reject(err);
  resolve(md5(buf));
})

Instead of the md5(buf) here what would i do to get the new name of the file? what im recieving now from the resolve is a new md5 name for the file, but before it has been resolved the filename is different.

4
  • Store the name in a variable. Commented Sep 18, 2018 at 19:43
  • How would i do that? Commented Sep 18, 2018 at 19:47
  • let filename = directoryPath + md5(buf + randomNumber()) + '.mp3'. Commented Sep 18, 2018 at 19:48
  • let filename = directoryPath + md5(buf + randomNumber()) + '.mp3'; fs.rename(file, directoryPath + md5(buf + randomNumber()) + '.mp3', (err) => { if (err) return reject(err); resolve(filename); }) like this? Commented Sep 18, 2018 at 19:52

1 Answer 1

2

Well, you can't!

What you can do, is first put the name in a variable so you can use it later!

Like this:

const renameTo = directoryPath + md5(buf + randomNumber()) + '.mp3';
fs.rename(file, renameTo,(err)=> {
  if (err) return reject(err);
  resolve(renameTo);
})

happy coding!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.