错误:Promise<any>'与类型'UseMutationOptions<unknown, Error, void, unknown>'没有共同属性

问题描述 投票:0回答:1

我正在使用 tanstack/react-query 中的 useMutation 挂钩将部门添加到我的数据库中。

这是 addDepartment 函数:

import { useQuery, useMutation } from '@tanstack/react-query';


  async function addDepartment(deptData: deptFormSchemaType): Promise<any> {
    const response = await fetch('/api/department', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(deptData),
    });
  
    if (!response.ok) {
      throw new Error('Failed to add department');
    }
  
    return response.json(); 
  }
  

这是 useMutation 钩子:

  const {mutate} = useMutation(addDepartment, {
    onSuccess: () => {toast.success('Department added successfully');
    form.reset();
    updateSelectedEmployees();
    setIsLoading(false);},
    onError: () => {toast.error('Error adding department');}
  })

错误出现在

useMutation(addDepartment
部分:

Type '(deptData: { name: string; description: string; dept_head: string; max_teams: number; members?: string[] | undefined; }) => Promise<any>' has no properties in common with type 'UseMutationOptions<unknown, Error, void, unknown>'.ts(2559)
(local function) addDepartment(deptData: deptFormSchemaType): Promise<any>
typescript promise tanstackreact-query
1个回答
0
投票

您的addDepartment函数需要添加为mutationFn。像这样

mutationFn:添加部门,

© www.soinside.com 2019 - 2024. All rights reserved.