【问题标题】:Why isn't the user I clicked deleted?为什么我点击的用户没有被删除?
【发布时间】:2022-01-11 13:34:59
【问题描述】:

我无法删除我在页面上选择的那个。每次列表中的第一个时它都会删除。

我不知道它是否与后端有关。如果是这种情况,您必须告诉我,然后我会在后端将其带入。

我的 UserManagement.js:

function UserManagement({ history }) {
 
  const dispatch = useDispatch();
  
  const userLogin = useSelector((state) => state.userLogin);
  const userList = useSelector((state) => state.userList);
  const { loading  } = userLogin;
  const { users} = userList;

 
  useEffect(() => {
    dispatch(getUsers());
  },[dispatch, history]);

  const deleteHandler = (id) => {
    if (window.confirm("Are you sure? you want to delete")) {
      dispatch(deleteUserAction(id));
    }
  };

  
  console.log(userList);
  console.log(users);
  return (
    <MainScreen title={`List of Users`}>
       <Link to="/createUser" id="OpenCreateUserDialogButton">
        <Button style={{ marginLeft: 10, marginBottom: 6 }} size="lg">
          Create new User
        </Button>
      </Link>
      
      {loading && <Loading />}
     
      {users &&
        users?.map((users) => (
         
          <Accordion>
            <Card style={{ margin: 10 }} key={users._id}>
              <Card.Header style={{ display: "flex" }}>
                <span
                  // onClick={() => ModelShow(note)}
                  style={{
                    color: "black",
                    textDecoration: "none",
                    flex: 1,
                    cursor: "pointer",
                    alignSelf: "center",
                    fontSize: 18,
                  }}
                >
                      
                      {users.userID}               
                </span>
                <div>
                  <Link to="/profileedit">
                    <Button
                    id="EditButton"
                    >Edit</Button>
                    </Link>
                    <Button
                      id="DeleteButton"
                      variant="danger"
                      className="mx-2"
                      onClick={() => deleteHandler(users.id)}
                    >
                      Delete
                    </Button>
                  </div>
              </Card.Header>

这是我的用户操作:

  export const deleteUserAction = (_id) => async (dispatch, getState) => {
    try {
      dispatch({
        type: USER_DELETE_REQUEST,
      });
  
      const {
        userLogin: { userInfo },
      } = getState();
  
      const url = "http://localhost:8080/user/";
  
      const config = {
        headers: {
          Authorization: `Bearer ${userInfo.token.token}`,
        },
      };
      console.log(userInfo.token.token);
      const { data } = await axios.delete(url, config);
  
      dispatch({
        type: USER_DELETE_SUCCESS,
        payload: data,
      });
    } catch (error) {
      const message =
        error.response && error.response.data.message
          ? error.response.data.message
          : error.message;
      dispatch({
        type: USER_DELETE_FAIL,
        payload: message,
      });
    }
  };

后端(服务器):

exports.deleteUser = async (request, response) => {
  try{
      await User.deleteOne({id: request.params.id});
      response.status(201).json("User deleted Successfully");
  } catch (error){
      response.status(409).json({ message: error.message});     
  }
}

【问题讨论】:

  • "Are you sure? you want to delete" - 你的问号放错地方了,应该在句末。
  • 您没有将用户的id 传递给axios.delete。你能发布你的后端代码吗?
  • @LucaPizzini 我现在将其编辑为我的代码,您可以照看它
  • @Phobos 句子结尾是什么意思?这是if条件问题..

标签: javascript reactjs


【解决方案1】:

您只是发送一个axios.delete() 呼叫请求。但是您没有将要实际删除的确切用户的任何标识符传递给该 API 请求。

您需要将标识符作为参数与您在axios.delete() 中传递的URL 一起传递。

例子:

      const url = "http://localhost:8080/user/$_id";

      ...

      const { data } = await axios.delete(url, config);
  
      ...

【讨论】:

  • 我已经尝试过了.. id 现在使用 localhost:8080/user/:id 但它不会改变任何东西
  • 在你的后端,如果你尝试console.log(request.params.id),它会打印id吗?如果不是,请在问题中附上User.deleteOne()的功能码。
  • 如果我删除了某个推送 Postman localhost:8080/user{用户的 id} 的人,那么它就被删除了
  • 你还没有回答我的问题。当您从 Postman 发送请求时,最希望 console.log(request.params.id) 打印 id。但是当你使用你的 React 应用发送请求时会打印出来吗?
猜你喜欢
  • 2015-12-10
  • 1970-01-01
  • 2012-09-22
  • 1970-01-01
  • 2019-12-07
  • 2010-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多