【发布时间】:2022-11-23 16:36:00
【问题描述】:
我想使用 rapidapi 使用 redux 获取一些数据,但是当我 console.log 数据时,我将得到 GET https://coinranking1.p.rapidapi.com/coins/coins 401(未经授权)。请问我做错了什么,有人帮帮我
这是我的 store.js 文件中的代码
import { configureStore } from "@reduxjs/toolkit"
import {cryptoApi} from "../services/cryptoApi"
export default configureStore({
reducer: {
[cryptoApi.reducerPath]: cryptoApi.reducer,
},
})
这是我存储从 rapidapi 获取的数据的地方
import { createApi, fetchBaseQuery} from "@reduxjs/toolkit/query/react"
const cryptoApiHeaders = {
'X-RapidAPI-Host': 'coinranking1.p.rapidapi.com',
'X-RapidAPI-Key': my-key
}
const baseUrl = 'https://coinranking1.p.rapidapi.com/coins'
const createRequest = (url) => ({url, Headers: cryptoApiHeaders })
export const cryptoApi = createApi({
ReducerPath: "cryptoApi",
baseQuery: fetchBaseQuery( { baseUrl } ),
endpoints: (builder) => ({
getCryptos: builder.query({
query: () => createRequest("/exchanges")
})
})
})
export const { useGetCryptosQuery, } = cryptoApi;
import { useGetCryptosQuery } from "../services/cryptoApi"
const {data, isFetching } = useGetCryptosQuery();
console.log(data);
【问题讨论】:
标签: reactjs redux-toolkit rapidapi