我想使用rapidapi使用redux获取一些数据,但当我控制台.log数据时,我将获得gethttps://coinranking1.p.rapidapi.com/coins/coins401(未经授权(。请问我做错了什么,有人帮我解决
这是我的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);
而不是这个:
const baseUrl = 'https://coinranking1.p.rapidapi.com/coins'
尝试这样更改:
const baseUrl = 'https://coinranking1.p.rapidapi.com'
还可以根据您需要的数据添加硬币或交易所。
如果您需要交易所的数据,请添加以下内容:
query: () => createRequest("/exchanges")
或者如果您需要硬币数据更改如下:
query: () => createRequest("/coins")