setting up routes order mern code example
Example: setting up routes order mern
router.patch(`/:id`, updatePost)
export const updatePost = async (req, res) => {
const { id: _id } = req.params;
const post = req.body;
if(!mongoose.Types.ObjectId.isValid(_id)) return res.status(404).send('No post with that id');
const updatedPost = await PostMessage.findByIdAndUpdate(_id, post, { new: true })
res.json(updatedPost)
}
const handleSubmit = (e) => {
e.preventDefault();
if(currentId) {
dispatch(updatePost(currentId, postData))
} else {
dispatch(createPost(postData))
}
};
export const updatePost = (id, updatedPost) => axios.patch(`${url}/${id}`, updatedPost)
export const updatePost = (id, post) => async (dispatch) => {
try {
const { data } = await api.updatePost(id, post);
dispatch({ type: `UPDATE`, payload: data });
} catch (error) {}
};
case "UPDATE":
return posts.map((post) => post._id === action.payload._id ? action.payload : post)