i want to update the progress value at the backend,it’s getting updated on using POSTMAN and sending this PATCH request
"http://localhost:5000/tasks/663c5474dbeffd71e2a501e2/progress?progress=80"
the front-end code is on click a button i want to update the already existing progress
i am using Axios to send the patch request
const handleSaveProgress = async () => {
if (selectedTaskIndex !== null && selectedTaskIndex >= 0 && selectedTaskIndex < updatedTasks.length) {
const updatedTask = updatedTasks[selectedTaskIndex];
const { progress: updatedProgress, _id } = updatedTask;
try {
const backendUrl = import.meta.env.VITE_BACKEND_URL || 'http://localhost:5000';
const response = await axios.patch(`${backendUrl}/tasks/${_id}/progress`, null, {
params: { progress: updatedProgress },
headers: {
'Content-Type': 'application/json',
},
});
if (response.status === 200) {
console.log('Progress updated successfully');
setUpdatedTasks([...updatedTasks]);
setShowModal(false);
} else {
console.error('Failed to update progress');
}
} catch (error) {
console.error('Error updating progress:', error);
}
}
};
the back-end code is
app.patch("/tasks/:id/progress",async(req,res)=>{
try{
const taskId = req.params.id;
const progress = req.query.progress;
console.log("Received progress:", progress);;
if (!progress || isNaN(progress) || progress < 0 || progress > 100) {
return res.status(400).json({ message: "Progress must be a number between 0 and 100" });
}
console.log("Received progress:", progress);
let status="Pending";
if(progress>0 && progress<100)
{ status="InProgress";}
else if(progress==100)
{ status="Completed";}
const updatedTask=await Task.findByIdAndUpdate(taskId,{status,progress},{new:true});
if(!updatedTask)
{
return res.status(404).json({message:"task not found"});
}
res.json({message:"Task Updated Successfully",task:updatedTask});
}
catch(error)
{
return res.status(500).json({error:error.message});
}
});
New contributor
Kunjal singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.