我有一些查询要从 mongo 中删除项目:
deleteCustomer: build.mutation({ query: (id) => ({ url: `client/cutomers/${id}`, method: 'DELETE', body: 'id', }), providesTags: ['DeleteUser'], }),
它在控制器中的外观:
export const deleteCustomer = async (req, res) => { try { const { id } = req.params; const formattedUsers = User.findByIdAndDelete(id); res.status(200).json(formattedUsers); } catch (e) { res.status(404).json({ message: e.message }) } }
路线如何:
import express from "express"; import { getEmployees, getCustomers, getTransactions, getGeography, deleteCustomer, getCustomer } from "../controllers/client.js"; const router = express.Router(); router.get("/employees", getEmployees); router.get("/customers", getCustomers); router.get("/transactions", getTransactions); router.get("/geography", getGeography); router.get("/customers/:id", getCustomer); router.delete("/customers/:id", deleteCustomer); export default router;
一旦我请求从 byza 中删除对象,我就会收到以下错误:
我已经验证使用 fetch 时获取请求可以正常工作。 这就是我提出请求的方式:
const [deleteCustomer] = useDeleteCustomerMutation() function handleDeleteUser(id) { // fetch('http://localhost:5001/client/customers/' + id, { // method: 'GET', // }) // .then(res => res.json()) // .then(res => console.log(res)) deleteCustomer(id) }
我相信我没有在控制器级别正确实现deleteUser中的删除功能。我非常感谢您关于解决此问题的建议:)
P粉9249157872024-04-02 14:11:31
查看您正在获取的路由 URL。在您的屏幕截图上,您当前发出的发布请求网址似乎是 http://localhost:5001/client/cutomers/...
我认为它在以下函数中:
deleteCustomer: build.mutation({ query: (id) => ({ url: `client/cutomers/${id}`, // You put cutomers instead of customers method: 'DELETE', body: 'id', }), providesTags: ['DeleteUser'], }),
这可以解释为什么你的服务器给出 404 not found 响应。
您可以尝试进行更正并向我们提供更新吗?