I have trouble with the response from the back-end.
I deleted an item with a specific _id using mongoose.
The create method is working well and I can get the value response and assign it into a variable.
Delete is also worked well but I can't get response value to assign the response to variable const post
function* deletePostSaga(action) {
try {
console.log("_id: ", action.payload);
const post = yield call(api.deletePost, action.payload);
console.log("Post - Delete: ", post);
yield put(actions.deletePost.deletePostSuccess(post));
} catch (err) {
console.error(err);
yield put(actions.deletePost.deletePostFailure(err));
}
}
The api.deletePost
:
import axios from "axios";
export const fetchPosts = () => axios.get(`/posts`);
export const createPost = (payload) => axios.post(`/posts`, payload);
export const updatePost = (payload) => axios.put("/posts", { data: payload });
export const deletePost = (payload) => {
console.log(payload);
axios.delete(`/posts`, { data: payload });
};
The item is deleted and I check the network from devtool and check the preview and I also got the response object:
message: "Delete successfully"
post: {author: "Anonymous", likeCount: 0, _id: "60d99146df51c152f189b1c4", title: "123", content: "123",…}
attachment: ""
author: "Anonymous"
content: "123"
createdAt: "2021-06-28T09:07:18.178Z"
likeCount: 0
title: "123"
updatedAt: "2021-06-28T09:07:18.178Z"
__v: 0
_id: "60d99146df51c152f189b1c4"
success: true
Just don't know why I couldn't assign the response to the const post
variable, it is just displayed as undefined
.
Thanks for reading.
deletePost = async (payload) => {
console.log(payload);
const response = await axios.delete(`/posts`, { data: payload });
return response;
};
While using async
key word you need to use await
in order to make an blocking call.