Nat
Nat

Reputation: 749

await is only valid in async function error

I am trying to do async and await in the product.findOneAndUpdate() but it seems that I am getting "await is only valid in async function error" for the await Product.findOneAndUpdate(). Here is my code. many thanks in advance and greatly appreciate any helps. Thanks

router.post('/product/saveeditproduct/:id',JWTAuthenticatToken, async (req, res) => { 
    let form = formidable.IncomingForm() 
    form.parse(req, (err,fields, files) => {
        if(err){           
            return res.json({statusCode: "400", msg:"upload denied"})
        }
    
        const{productname, productdescription} = fields
    const productslug = slugify(productname)
        const{image} = files

        const product= await Product.findOneAndUpdate({productslug:req.params.id},
            {$set:{productname:productname,productdescription:productdescription}},{new:true})
             
        if(image){

                 //---Remove old image from AWS S3---
                const deleteParams ={
                    Bucket:process.env.AWS_BUCKET_NAME,
                    Key:`image/${product.productslug}`,
                    Body:fs.readFileSync(image.path),
                    ACL:'public-read',
                    ContentType:`image/jpg`
                }
    
               
                s3.deleteObject(deleteParams,(err,data) => {
    
                })
                //---Remove old image from AWS S3---
                
                //----Upload new image to AWS S3----
                const params ={
                    Bucket:process.env.AWS_BUCKET_NAME,
                    Key:`image/${productslug}`,
                    Body:fs.readFileSync(image.path),
                    ACL:'public-read',
                    ContentType:`image/jpg`
                }
        
                s3.upload(params, async(err,data) => {
                            if(err) {
                                res.json({status:true, error:err})
                            } else{
                                product.productimageurl = data.Location
                                const productresult = await product.save()
                                return res.json({statusCode: "200", data:productresult})
                            }
                        })
                }
                //----Upload new image to AWS S3----

                  return res.json({statusCode: "200"})
        })

})

Upvotes: 1

Views: 111

Answers (2)

Yatinj Sutariya
Yatinj Sutariya

Reputation: 522

You should always use async with await.

You made the outer function async:

router.post('/product/saveeditproduct/:id',JWTAuthenticatToken, async (req, res) => {

});

But you forgot to add async in the inner function (Parent of that particular await).

Solution is to make that function async:

form.parse(req, async (err, fields, files) => {
     
}

Upvotes: 2

Parth Tagadiya
Parth Tagadiya

Reputation: 174

I think you forget to add async at :

form.parse(req, async (err, fields, files) => {
     //code....
}

Upvotes: 4

Related Questions