• 主页
  • 我收到对象(...)当我从api使用useGetCryptoNewsQuery并尝试console.log(cryptoNews)时,不是函数

我收到对象(...)当我从api使用useGetCryptoNewsQuery并尝试console.log(cryptoNews)时,不是函数

我尝试从快速API bing新闻中获取数据,但当我试图将其提取出来时,它显示console.log (...)不是函数。当我尝试删除News.js的第6行时,它会呈现出来,但是如果我把它放回去,它会返回同样的问题

News.js

import { Select, Typography } from 'antd'
import { useGetCryptoNewsQuery } from '../services/cryptoNewsApi'
const {Text, Title} = Typography
const { Option } = Select
const News = ({simplified}) => {
    const { data: cryptoNews} = useGetCryptoNewsQuery({newsCategory: 'Cryptocurrency', count: simplified ? 6 : 12})
    console.log(cryptoNews)
    return(
        <div>
            <h1>Hello</h1>
        </div>
    )
     
    
}

export default News

store.js

import { configureStore } from '@reduxjs/toolkit'
import { cryptoApi } from '../services/cryptoApi'
import { cryptoNewsApi } from '../services/cryptoNewsApi'

export default configureStore({
  reducer: {
    [cryptoApi.reducerPath]: cryptoApi.reducer,
    [cryptoNewsApi.reducerPath]: cryptoNewsApi.reducer,
  },
})

cryptoNewsApi.js

import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/dist/query'

const cryptoNewsHeaders = {
  'x-bingapis-sdk': 'true',
  'x-rapidapi-host': 'bing-news-search1.p.rapidapi.com',
  'x-rapidapi-key': 'API-KEY',
}

const baseUrl = 'https://bing-news-search1.p.rapidapi.com'

const createRequest = (url) => ({ url, headers: cryptoNewsHeaders })

export const cryptoNewsApi = createApi({
  reducerPath: 'cryptoNewsApi',
  baseQuery: fetchBaseQuery({ baseUrl }),
  endpoints: (builder) => ({
    getCryptoNews: builder.query({
      query: ({ newsCategory, count }) =>
        createRequest(
          `/news/search?q=${newsCategory}&safeSearch=Off&textFormat=Raw&freshness=Day&count=${count}`
        ),
    }),
  }),
})

export const { useGetCryptoNewsQuery } = cryptoNewsApi

转载请注明出处:http://www.jxbyjx.net/article/20230504/1979107.html