Problem with upload file / triggerUncaughtException

Viewed 2500

Below is my code. I have a problem with the line:

console.log(await fileTypeFromFile(file.originalFilename));

I get the error:

node:internal/process/promises:279
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[Error: ENOENT: no such file or directory, stat 'upload.jpeg'] {

But I send this file to the server, and uploaded it to the folder upload, what I should correct?

...
app.post("/api/upload", (req, res, next) => {
  const form = formidable({
    multiples: true,
    uploadDir: "./uploads",
  });

  const uploadDirIndex = fs.readdirSync(form.uploadDir).length + 1;

  form.on("file", async (field, file) => {
    console.log(await fileTypeFromFile(file.originalFilename));

    const fileExtension = path.extname(file.originalFilename);
    const fileNewName = `avatar-${uploadDirIndex}${fileExtension}`;

    fs.rename(file.filepath, form.uploadDir + "/" + fileNewName, () => {
      console.log(`test ${file.originalFilename.split(" ").join("")}`);
      console.log(file.name);
    });
  });

  form.parse(req, (err, fields, files) => {
    if (err) {
      next(err);
      return;
    }
    res.json({ fields, files });
  });
});
...
0 Answers
Related