How to set error in Redux toolkit array if no data returned from API

Viewed 20

i started implementing RTK QUERYnow i am facing one issue , i have an API,the API returns user Settings. Now from API if the there is data related to user it returns success=1 otherwise success=0 , but in both cases it returns response status 200. Now i want to show error to user if sucess=0 and set the set the error in the RTk Error object. How can i implement that.

import { apiSlice } from "../../services/ApiSlice";
import * as URL from "../Urls";
export const userSlice = apiSlice.injectEndpoints({
  endpoints: (builder) => ({
    getUserSettings: builder.mutation({
      query: (body) => ({
        url: URL.GET_SETTINGS,
        method: "POST",
        body: body,
        responseHandler: (response) => response.json(),

        validateStatus: (response, result) =>
          response.status === 200 ? result : "error in fetching data",
      }),
      transformResponse: (response) => {
        console.log({ response });
        if (response.success === 0) return "Error in fetching data";
      },
      providesTags: (result, error, arg) => {
        if (result) {
          console.log(result);
          return [...result.ids.map((id) => ({ type: "users", id })), "users"];
        } else return ["users"];
      },
    }),
  }),
});

export const { useGetUserSettingMutation } = userSlice;
0 Answers
Related