jeudi 22 septembre 2022

Deleted users disappearing after refreshing the page not real time in react

I wanted to select multiple users from table and delete them with real time . When i check db it is deleting selected users but from front end with react it is happening after refreshing the page. Can anyone help me? Here is my FE code:

const handleDeleteUser = async () => {
    const response = await fetch(`http://localhost:3001/users/deleteUsers`, {
      method: "DELETE",
      body: JSON.stringify(isChecked),
      headers: {
        "Content-Type": "application/json",
      },
    });
    if(response.status === 200){
      const data = await response.json()
      for (let i = 0; i < data.length; i++) {
        const element = data[i];
        setUsers(users.filter((e) => e._id !== element))
        console.log("DELETED ELEMENT",element);
      }
    }
  };

BE code:

userRouter.delete("/deleteUsers", async (req, res, next) => {
  try {
    const selectedUsers = req.body;
    // HARD DELETE
    UsersModel.deleteMany(
      {
        _id: {
          $in: selectedUsers,
        },
      },
      function (err, result) {
        if (err) {
          res.status(404).send(err);
        } else {
          res.status(200).send(selectedUsers);
        }
      }
    );
  } catch (error) {
    next(error);
  }
});




Aucun commentaire:

Enregistrer un commentaire