0

I am using this library for file uploading. Here it says

 sampleFile.mv('/somewhere/on/your/server/filename.jpg', function(err) {
    if (err)
      return res.status(500).send(err);

    res.send('File uploaded!');
  });

But I want to use this using await and async so when I try like

router.put("/upload", async(req,res)=>{
    const isUploaded = await sampleFile.mv('/somewhere/on/your/server/filename.jpg');
    console.log(isUploaded) // it gives me undefined over here.
});
1
  • the first example is a callback, not a promise. Commented Sep 1, 2020 at 4:12

2 Answers 2

1

I have gone through the code of .mv() of the library that you are using. It does have promise support.

However, it seems like it resolves() with empty argument.

So, if you want to use async await, you can use,

router.put("/upload", async(req,res)=>{
    try{
         await sampleFile.mv('/somewhere/on/your/server/filename.jpg');
         res.send('File uploaded!');
    } catch(err){
         res.status(500).send(err);
});

You cannot use

const isUploaded = await mvPromise('/somewhere/on/your/server/filename.jpg');

It will always be undefined because it does not return anything.

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

5 Comments

The above example wouldn't work because you are not properly pulling sampleFile from the request. It would basically throw because sampleFile is not defined. Also the catch statement is not properly written so the above would fail for that as well.
The sampleFile will be imported properly, the code I posted is not the full code. Jass, who asked the question knows it. I just provided the necessary and the part requested.
It’s always good to provide running samples or complete proper snippets without errors. Stack overflow is not just for the person who asked the question but also for future users. They probs won’t know everything so it’s good to provide as much info as you can. Also what is mvPromise? That’s not referenced anywhere as a function, so it’s providing confusing information
I usually take care of that. Also, I try to be specific to the question asked. Sometimes, more details leads to confusion. This question is specific to a particular library. Anybody looks here would know at least how to use the library. Thanks though.
got it that it returns nothing so need to send response in try catch block
1

Library supports promises when no callback is provided so you should be able to just await it

Resource: https://github.com/richardgirges/express-fileupload/blob/1216f4f0685caca7f1ece47f52c6119dc956b07d/lib/fileFactory.js#L62

To access the proper file create in middleware you need to look at the request. Also the response of the function mv is not supposed to return anything. As long as it doesn't throw then you're good to go.

    app.post("/upload", async (req, res) => {
      if (!req.files || Object.keys(req.files).length === 0) {
        return res.status(400).send("No files were uploaded.");
      }

      // The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
      let { sampleFile } = req.files;

      // Use the mv() method to place the file somewhere on your server
      try {
        await sampleFile.mv("/somewhere/on/your/server/filename.jpg");
        res.send("File uploaded!");
      } catch (e) {
        res.sendStatus(500);
      }
    });

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.