redux RTK查询未刷新-为什么?

gg0vcinb  于 2022-11-12  发布在  其他
关注(0)|答案(1)|浏览(214)

我有一个RTK查询在删除变异后没有刷新它的核心内容。有人能解释为什么吗?我不清楚问题出在哪里,因为在任何时候都没有刷新请求。
代码看起来很好,和我在另一个工作的API中使用的代码几乎一样,在第二个API上,我传递了相同的数据(一个项目列表),在删除后它可以很好地刷新;下面是代码:

import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import { Auth, API, Storage } from 'aws-amplify';

// Define a service using a base URL and expected endpoints
export const researchApi = createApi({
    reducerPath: 'researchApi',
    tagTypes: ['Research'],

    baseQuery: fetchBaseQuery({ 
        baseUrl: process.env.NEXT_PUBLIC_API_RESEARCH,
        prepareHeaders: async (headers, { getState }) => {
            const token = (await Auth.currentSession()).getIdToken().getJwtToken();
            headers.set('Authorization', `${token}`);     
            headers.set('Content-Type', 'application/json');       
            return headers;
        }
    }),
    endpoints: (builder) => ({

      getResearch: builder.query({
        query: () => `research`,
        providesTags: ['Research']
      }),

      getResults: builder.query({
        query: (id) => `results?searchid=${id}`,
      }),

      addResearch: builder.mutation({
        query(keywords) {
            const data = {
                keywords: keywords
            }
          return {
            url: `research`,
            method: 'POST',
            body: data
          }
        },
        invalidatesTags: ['Research']
      }),

      deleteResults: builder.mutation({
        query(results) {

            // send array
            let sanitized;
            sanitized = results.filter(item => item);

            const data = {
                items: sanitized
            }
            //console.log('data: ', data);
          return {
            url: `removeresult`,
            method: 'DELETE',
            body: data
          }
        },
        invalidatesTags: ['Research']
      }),

    }),
  })

// Export hooks for usage in functional components, which are
// auto-generated based on the defined endpoints
export const {  useGetResearchQuery, useGetResultsQuery, useAddResearchMutation, useDeleteResultsMutation } = researchApi

我这样调用查询:

const router = useRouter()
    const { kwd } = router.query
    const { data, error, isError, isLoading } = useGetResultsQuery(kwd);

    if(isLoading) {
        return ( 
            <>
                <Spinner animation="border"  size="sm" role="status" />{' '} Please wait while Loading...
            </>
        )
    }

任何想法都将是非常有帮助的,因为我完全迷失了这个...

pwuypxnk

pwuypxnk1#

还好问题就这样解决了,我没有添加正确的参数:
获取研究:生成器。查询({ query:()=〉research,提供标签:['研究'] }),

getResults: builder.query({
    query: (id) => `results?searchid=${id}`,
     providesTags: ['Research'] // ========> THAT WAS MISSING
  }),

相关问题