我正在使用RTK-Query和Redux-toolkit用于这个应用程序,并且我根据文档使用createApi创建了一个api-slice。
当我向后端运行一个请求时,我得到一个"FETCH_ERROR"
;然而,当我使用Axios运行相同的请求时,我从后端正确地获得了数据,这使我相信我的代码中有错误。我只是不确定它到底在哪里。
下面是错误:
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 [],
},
}
下面是测试切片:
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;
下面是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;
2条答案
按热度按时间h9vpoimq1#
我解决了这个问题,将后端URL更改为我当前的ipv4(用于expo开发,否则只是你的后端URL)地址在我的.env文件中,然后删除缓存,并重新启动我的应用程序。在我的情况下,我使用expo so,
expo r -c
,它工作了。oknwwptz2#
我通过将我的后端URL http://localohost:6464替换为当前的ipv4 wi-fi网络http://192.168.0.133:6464解决了这个问题,你可以在wi-fi属性中找到你的ipv4,然后用命令重新启动应用程序:npm start --reset-cache或npx expo start -c