2

I am using RTK-Query, and Redux-toolkit for this app, and I created an api-slice with createApi, as per the docs.

When I run a request to the backend, I get a "FETCH_ERROR"; however, when I run the same request using Axios, I get the data correctly from the backend, which leads me to believe I have an error in my code. I am just not sure where exactly it is.

Here is the error:

Object {
  "api": Object {
    "config": Object {
      "focused": true,
      "keepUnusedDataFor": 60,
      "middlewareRegistered": true,
      "online": true,
      "reducerPath": "api",
      "refetchOnFocus": false,
      "refetchOnMountOrArgChange": false,
      "refetchOnReconnect": false,
    },
    "mutations": Object {},
    "provided": Object {},
    "queries": Object {
      "test(undefined)": Object {
        "endpointName": "test",
        "error": Object {
          "error": "TypeError: Network request failed",
          "status": "FETCH_ERROR",
        },
        "requestId": "BWOuLpOxoDKTzlUYFLW4x",
        "startedTimeStamp": 1643667104869,
        "status": "rejected",
      },
    },
    "subscriptions": Object {
      "test(undefined)": Object {
        "QJSCV641RznGWyudGWuMb": Object {
          "pollingInterval": 0,
          "refetchOnFocus": undefined,
          "refetchOnReconnect": undefined,
        },
      },
    },
  },
  "test": Object {
    "data": Array [],
  },
}

Here is the test slice:

import { createSlice } from "@reduxjs/toolkit";

const testSlice = createSlice({
  name: "test",
  initialState: {
    data: [],
  },
  reducers: {
    getData: (state) => {
      state;
    },
  },
});

export const { getData } = testSlice.actions;
export default testSlice.reducer;

Here is the apiSlice:

import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";

export const apiSice = createApi({
  reducerPath: "test",
  baseQuery: fetchBaseQuery({ baseUrl: process.env.REACT_APP_backend_url }),
  endpoints: (builder) => ({
    test: builder.query({
      query: () => "/test",
    }),
  }),
});

export const { useTestQuery } = apiSice;
3
  • Well. The network request failed. Without sharing any code it's impossible to do more than read the error message to you. Commented Jan 31, 2022 at 22:40
  • Added the code. Commented Jan 31, 2022 at 22:52
  • I believe it was a problem with the env URL being incorrect. I had to clean cache and restart. Is this question still relevant to leave up? Commented Jan 31, 2022 at 23:12

3 Answers 3

4

I solved it by changing the backend URL to my current ipv4 (for expo development, otherwise just your whatever your backend URL is) address in my .env file, then deleting cache, and restarting my app. In my case I was using expo so, expo r -c, and it worked.

Sign up to request clarification or add additional context in comments.

Comments

0

I solved by replacing my backend url http://localohost:6464 to the current ipv4 wi-fi network http://192.168.0.133:6464 you can find your ipv4 in wi-fi properties then restart the app with command: npm start --reset-cache or npx expo start -c

Comments

0

For Wi-Fi connection

Select Start > Settings > Network & internet > Wi-Fi and then select the Wi-Fi network you're connected to.

Under Properties, look for your IP address listed next to IPv4 address.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.