You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use case (Sequential Execution with plain callback function and async-await) - Here in the below EDIT / PUT route - From Client side req.body, I am sending a JSON data which along with other info has a 'date' field. I had to format that 'date' from req.body (to "YYYY-MM-DD") with moment, before running findByIdAndUpdate(). Else mongoose was saving the date one day prior to what I was selecting in the DatePicker, and then subsequent API call for that date's data (after the EDIT) will NOT give me the correct edited data.
NOTE - The code is working EVEN WITHOUT async-await. But including async-await seems to be better for safely achieving the result.
Alternative - 1 - With a wrapper function which will invoke a callback()
router.put("/:id",(req,res,next)=>{leteditedItem=req.body;// Have to do this extra date formatting step, to change the formatting of the 'date' field. Else mongoose was saving the date one day prior to what I was selecting in the DatePicker.// Wrapper function that will wrap the database call (the findByIdAndUpdate() query) within a callback function that gets executed after the database query has finished.constwrapperFunction=async(editItem,callback)=>{editItem.date=awaitmoment(editItem.date).format("YYYY-MM-DD");if(typeofcallback==="function"){callback();}};wrapperFunction(editedItem,()=>{Revenue.findByIdAndUpdate(req.params.id,editedItem,{new: true}).exec((err,record)=>{if(err){console.log(err);returnnext(err);}else{res.status(200).json(record);}});});});
Alternative - 2 for the above EDIT (PUT) route function (with an extra database call ) - this code was working
Alternative-2 for the EDIT (PUT) route function above - Here, I am just plain formatting the date without resorting to any technique to make the next db-call function to wait. While its working the solution with async-await seems to be better for safely achieving the same.