MERN删除后端未定义的数据
MERN delete data undefined at backend
我正在尝试在我的 MERN 应用程序中实现删除功能,但每当我尝试将我的 delete IDs
发送到后端控制器时,Delete ID Array
未定义
deletePosts
是要删除的ID数组 deletePosts = [id1, id2, id3 ...]
React 帖子组件:-
function AllPosts() {
const dispatch = useDispatch();
const { posts, isSuccess } = useSelector((state) => state.posts);
const handleDelete = () => {
dispatch(deletePost({deletePosts}));
};
return (
<>
{posts.map((post, index) => (
<Post
postId={post._id}
subject={post.subject}
handleDelete={handleDelete}
/>
))}
</>
)}
export default AllPosts;
Redux 工具包切片:-
export const deletePost = createAsyncThunk(
"posts/delete",
async (postDelete, thunkAPI) => {
try {
const token = JSON.parse(localStorage.getItem("token"));
return await postService.deletePost(postDelete, token);
} catch (error) {
const message =
(error.response &&
error.response.data &&
error.response.data.message) ||
error.message ||
error.toString();
return thunkAPI.rejectWithValue(message);
}
}
);
REDUX 工具包服务:-
const deletePost = async (postDelete, token) => {
const config = {
headers: {
Authorization: `Bearer ${token}`,
},
};
const response = await axios.delete(API_URL, postDelete, config);
return response.data;
};
路线:-
router.route("/").delete(protect, deletePost)
控制器:-
const deletePost = asyncHandler(async (req, res) => {
console.log("Delete Controller:- ", req.body);
/****
*
* Delete Logic
*
*
*/
});
控制器 console.log:-
Delete Controller:- {}
数据来自Component > Slice > Service > Route > Controller
the deletePosts array has content of Ids till REDUX toolkit Service
但在控制器中显示 Undefined
也许这会解决您的问题:
const deletePost = async (postDelete, token) => {
const config = {
headers: {
Authorization: `Bearer ${token}`,
},
data:{
postDelete
}
};
const response = await axios.delete(API_URL, config);
return response.data;
};
此外,您正在将已删除的内容作为对象发送,如果这不是故意的,请更改您的 React 帖子组件中的以下内容
const handleDelete = () => {
dispatch(deletePost(deletePosts)); // ({deletePosts}) will give you an object of deleted posts in the destination instead of an array
};
我正在尝试在我的 MERN 应用程序中实现删除功能,但每当我尝试将我的 delete IDs
发送到后端控制器时,Delete ID Array
未定义
deletePosts
是要删除的ID数组 deletePosts = [id1, id2, id3 ...]
React 帖子组件:-
function AllPosts() {
const dispatch = useDispatch();
const { posts, isSuccess } = useSelector((state) => state.posts);
const handleDelete = () => {
dispatch(deletePost({deletePosts}));
};
return (
<>
{posts.map((post, index) => (
<Post
postId={post._id}
subject={post.subject}
handleDelete={handleDelete}
/>
))}
</>
)}
export default AllPosts;
Redux 工具包切片:-
export const deletePost = createAsyncThunk(
"posts/delete",
async (postDelete, thunkAPI) => {
try {
const token = JSON.parse(localStorage.getItem("token"));
return await postService.deletePost(postDelete, token);
} catch (error) {
const message =
(error.response &&
error.response.data &&
error.response.data.message) ||
error.message ||
error.toString();
return thunkAPI.rejectWithValue(message);
}
}
);
REDUX 工具包服务:-
const deletePost = async (postDelete, token) => {
const config = {
headers: {
Authorization: `Bearer ${token}`,
},
};
const response = await axios.delete(API_URL, postDelete, config);
return response.data;
};
路线:-
router.route("/").delete(protect, deletePost)
控制器:-
const deletePost = asyncHandler(async (req, res) => {
console.log("Delete Controller:- ", req.body);
/****
*
* Delete Logic
*
*
*/
});
控制器 console.log:-
Delete Controller:- {}
数据来自Component > Slice > Service > Route > Controller
the deletePosts array has content of Ids till REDUX toolkit Service
但在控制器中显示 Undefined
也许这会解决您的问题:
const deletePost = async (postDelete, token) => {
const config = {
headers: {
Authorization: `Bearer ${token}`,
},
data:{
postDelete
}
};
const response = await axios.delete(API_URL, config);
return response.data;
};
此外,您正在将已删除的内容作为对象发送,如果这不是故意的,请更改您的 React 帖子组件中的以下内容
const handleDelete = () => {
dispatch(deletePost(deletePosts)); // ({deletePosts}) will give you an object of deleted posts in the destination instead of an array
};