首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用多个参数对providesTags函数抛出错误来响应Redux查询

用多个参数对providesTags函数抛出错误来响应Redux查询
EN

Stack Overflow用户
提问于 2022-03-12 16:10:24
回答 1查看 1.6K关注 0票数 0

我目前正在将React实现到我的中,并且我在获取数据方面遇到了问题。

我想要创建一个端点,查询需要多个参数才能工作。例如,端点是https://localhost:8080/object/id,其中对象和Id是我需要从其中获取数据的参数。

我为查询创建了如下接口:

代码语言:javascript
复制
export interface ISearch {
    objectName: string,
    id: string
}

我的apiSlice看起来是这样的:

代码语言:javascript
复制
import {createApi, fetchBaseQuery} from "@reduxjs/toolkit/query";
import {ISearch} from "../utils/Interfaces";

export const apiSlice = createApi({
    reducerPath: 'api',
    baseQuery: fetchBaseQuery({
        baseUrl: 'https://localhost:8080/api/v1'
    }),
    tagTypes: ['Record'],
    endpoints: builder => ({
        getRecord: builder.query({
            query: (params: ISearch) => `/${params.objectName}/${params.id}`,
            providesTags: (result, error, arg) => [{
                type: 'Record',
                id: arg
            }]
        })
    })
})

唯一的问题是,我在providesTags函数上得到了以下错误:

代码语言:javascript
复制
TS2322: Type '(result: any, error: FetchBaseQueryError, arg: ISearch) => { type: "Record"; id: ISearch; }[]' is not assignable to type 'ResultDescription<"ObjectDefinition" | "Record", any, ISearch, FetchBaseQueryError, FetchBaseQueryMeta>'.   Type '(result: any, error: FetchBaseQueryError, arg: ISearch) => { type: "Record"; id: ISearch; }[]' is not assignable to type 'GetResultDescriptionFn<"ObjectDefinition" | "Record", any, ISearch, FetchBaseQueryError, FetchBaseQueryMeta>'.     Type '{ type: "Record"; id: ISearch; }[]' is not assignable to type 'readonly TagDescription<"ObjectDefinition" | "Record">[]'.       Type '{ type: "Record"; id: ISearch; }' is not assignable to type 'TagDescription<"ObjectDefinition" | "Record">'.         Type '{ type: "Record"; id: ISearch; }' is not assignable to type 'FullTagDescription<"ObjectDefinition" | "Record">'.           Types of property 'id' are incompatible.             Type 'ISearch' is not assignable to type 'string | number'.               Type 'ISearch' is not assignable to type 'number'.
EN

回答 1

Stack Overflow用户

发布于 2022-03-12 22:21:19

查询标记由类型和字符串id组成。arg在您的providesTags函数中是您在query函数中所称的params。因此,与其在那里使用arg作为id,不如使用arg.id或像arg.object + '/' + arg.id这样的连接字符串。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71451175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档