0

I am new to react query and tried many tutorials still I am not logging anything and nothing happens after the following code, cannot even log new data, It doesnt feel like there is any error. POST is working perfectly.

   addCategory: builder.mutation({
  query: (body) => ({
    method: "POST",
    url: apiLinks.categoryPUT,
    body: body,
  }),
  async onQueryStarted(body, { dispatch, queryFulfilled }) {
    try {
      const res = await queryFulfilled;
      const id = _.get(res, "data.data.InsertedID", Date.now());

      dispatch(
        categoryApi.util.updateQueryData(
          "getAllCategory",
          undefined,
          (data) => {
            const newBody = { id, ...body };
            console.log({ data, newBody });  //nothing shows here
            data.push(newBody);
          }
        )
      );
    } catch (error) {
      console.log({ error });  // nothing logs here
    }
  },
}),
Sushilzzz
  • 527
  • 5
  • 20

1 Answers1

0

problem was I used this: useGetAllCategoryQuery("getAllCat"); to fetch data.

useGetAllCategoryQuery("getAllCat"); 

so it should be:

categoryApi.util.updateQueryData(
          "getAllCategory",
          "getAllCat", //change here 

seems its a unique identifier for cache data

Sushilzzz
  • 527
  • 5
  • 20