Такая ошибка:
Вот функция которая посылает гет запрос на апи:
export const getWeather = async (units: 'metric' | 'imperial' | 'standard', cityname: string) => {
const { data } = await axios.get<WeatherItem[]>(`${DEFAULT_PATH_API}/data/2.5/weather?q=${cityname}&units=${units}&appid=${API_KEY}`);
return data;
}
Async Action:
export const fetchWeather = createAsyncThunk(
'weather/fetchWeather',
async (params: FetchWeatherProps) => {
const { units = 'metric', cityname = 'Kyiv' } = params;
const weather = await getWeather(units, cityname);
return weather;
}
)
Слайс:
const initialState: WeatherSliceState = {
items: [],
status: Status.LOADING
}
export const WeatherSlice = createSlice({
name: 'weather',
initialState,
reducers: {
},
extraReducers: (builder) => {
builder.addCase(fetchWeather.pending, (state) => {
state.items = [];
state.status = Status.LOADING;
})
builder.addCase(fetchWeather.fulfilled, (state, action: PayloadAction<WeatherItem[]>) => {
state.items = action.payload;
state.status = Status.SUCCESS;
})
builder.addCase(fetchWeather.rejected, (state) => {
state.items = [];
state.status = Status.ERROR;
})
}
})
export default WeatherSlice.reducer;
Типы к слайсу:
export interface WeatherSliceState {
items: WeatherItem[];
status: Status.LOADING | Status.ERROR | Status.SUCCESS;
}
export type WeatherItem = {
weather: {
icon: string;
main: string;
};
main: {
temp: number;
feels_like: number;
humidity: number;
pressure: number;
temp_min: number;
temp_max: number;
};
name: string;
visibility: number;
wind: {
speed: number;
};
sys: {
sunrise: number;
sunset: number;
country: string;
};
}
export enum Status {
LOADING = 'loading',
SUCCESS = 'success',
ERROR = 'error'
}
Вот этот код выдаёт корректный ответ от сервера в консоль:
const weather = useSelector(weatherSelector);
const status = useSelector(statusSelector);
console.log(weather);