3

我有一个根 API,我在其中注入端点。但是当我尝试添加一个转换函数时它不起作用。从 transformResponse 的文档中,我可以看到 transform 道具附加到查询对象。

这是我正在尝试的

const departmentsApi = onboardApi.injectEndpoints({
    endpoints: builder => ({
        getDepartments: builder.query({
            query: params => ({
                url: `departments`,
                params,
                // Transform and normalize API response
            }),
            transformResponse: response => {
                 console.log('transform', response);
                 return response;
            },
            providesTags: result => {
                return result
                    ? [
                            ...result.items.map(({ id }) => ({ type: 'Department', id })),
                            { type: 'Department', id: 'LIST' },
                      ]
                    : [{ type: 'Department', id: 'LIST' }];
            },
        }),
    }),
    overrideExisting: false,
});

任何指导都会有所帮助。

资源

  1. 注入端点的文档
4

1 回答 1

3

您链接了上面的 Redux-Query 文档,而不是 RTK-Query。transform不是一回事。你可能想要transformResponse

getDepartments: builder.query({
    query: params => ({
        url: `departments`,
        params,
        // Transform and normalize API response
        transform: response => {
            console.log(response);
            return response;
        },
    }),
    transformResponse: (response) => response.some.deeply.nested.collection,
    providesTags: result => {
        return result
            ? [
                    ...result.items.map(({ id }) => ({ type: 'Department', id })),
                    { type: 'Department', id: 'LIST' },
              ]
            : [{ type: 'Department', id: 'LIST' }];
    },
}),
于 2021-10-11T08:33:19.853 回答