1

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"})
        })

})

2 Answers 2

4

I think you forget to add async at :

form.parse(req, async (err, fields, files) => {
     //code....
}
Sign up to request clarification or add additional context in comments.

Comments

2

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) => {
     
}

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.