0

I am working with Nodejs,I am using expressjs, I am new in express, Right now i am creating an "API",But i am getting following error await is only valid in async function Here is my current code,How can i fix this issue

const UploadImage = async (req, res) => {
 uploadUserImage(req,res,function(err) {
         const file = req.file;
         const UserId=req.body.UserId;
        console.log(file);  // Getting file information
        if (!file) {
                return res.status(400).send({ message: 'Please upload a file.' });
            }
        if(!salonId)
        {
             return res.status(400).send({ message: 'Please enter shopId' });
        }    
        const result= await User.addVendorGalleryImages(req.file.filename,salonId);
        
        res.end("File is uploaded"+req.file.filename);
    });

    }

Stuart
  • 17
  • 2
  • Although you did mark the outer function as `async`, note that your code has several functions. You should mark the inner function as `async` as well if you want to use the await keyword – Allan Juan May 17 '23 at 16:54
  • What is `salonId`? Does `addVendorGalleryImages` return a promise? – trincot May 17 '23 at 17:04
  • @trincot by mistake , please ignore – Stuart May 17 '23 at 17:05
  • 1
    What is the purpose of `result`? It is not used... Can you please edit your question and remove undefined variables and clarify `result`? – trincot May 17 '23 at 17:09
  • @trincot result means , whenever we get response from model(query) – Stuart May 17 '23 at 17:22
  • But you throw away that `result`. Please edit your question and remove undefined variables and variables you don't use. – trincot May 17 '23 at 17:33
  • add await code : `await uploadUserImage(req,res,async function(err)` – perona chan May 17 '23 at 17:39
  • 2
    @peronachan, no that will not help. – trincot May 17 '23 at 17:40
  • all it asks for is the `async await` code. does it have to help with coding structures like `pro player`? – perona chan May 17 '23 at 17:48
  • 1
    @peronachan, I think you miss the point. `uploadUserImage` does not return a promise, so `await` is useless with it. It uses a callback pattern for indicating the asynchronous task has finished, not a promise. – trincot May 17 '23 at 17:51
  • @trincot can you please update/post the answer so i can check, i am stuck with this issue from last three days – Stuart May 17 '23 at 17:52
  • Sorry, but this question is not answerable in the state it is in. Please see my comments above, and edit your question. – trincot May 17 '23 at 17:59
  • @trincot i just want to upload image using API in expressjs , if you can help then post your answer otherwise no problem,Thank you for your comments – Stuart May 17 '23 at 18:03
  • this might help https://pqina.nl/blog/upload-image-with-nodejs/ – perona chan May 17 '23 at 18:19
  • You need to [convert the callback API to promises](https://stackoverflow.com/q/22519784/1048572). Only then you can use `await`. Is `uploadUserImage` a function written by you? Then please post its code – Bergi May 17 '23 at 20:16
  • Does this answer your question? [await is only valid in async function](https://stackoverflow.com/questions/49432579/await-is-only-valid-in-async-function) – Robert Bradley May 17 '23 at 21:55

0 Answers0