简体   繁体   English

在 GraphQL 查询之后:属性 'then' 在类型 'void' 上不存在

[英]After GraphQL Query: Property 'then' does not exist on type 'void'

I was using graphql mutations like this and the.then &.catch work perfectly:我正在使用这样的 graphql 突变,然后 &.catch 完美地工作:

  let submitForm = (
    email: string,
    firstName: string
  ) => {
    setIsSubmitted(true);

    if (email && (firstName)) {
      const input: UpdateUserInput = {};
      if (firstName) {
        input.firstName = firstName;
      }
      updateUser({
        variables: {
          email: email,
          input: input,
        },
      })
        .then(({ data }: ExecutionResult<UpdateUserResponse>) => {
          if (data !== null && data !== undefined) {
            setIsUpdated(true);
          }
        })
        .catch((error: { message: string }) => {
          console.log('Error msg:' + error.message);
        });
    }
  };

Now I am doing something similar here for a graphql query (fuller working version below):现在我正在为 graphql 查询做类似的事情(下面更完整的工作版本):

let ShowUsers = () => {
    const where: WhereInput = {};
    if (criteria === '2') {
      if (searchItem) {
        where.firstName_contains = searchItem;
        loadUsers({
          variables: {
            where: where
          },
        })
        .then(({ data }: any) => {
          if (data !== null && data !== undefined) {
          }
        })
      }
    }
}

but I keep getting an error on then that Property 'then' does not exist on type 'void'但我一直收到错误消息,即Property 'then' does not exist on type 'void'

Edit:编辑:

Without the.then, .catch, my code works correctly.如果没有.then、.catch,我的代码可以正常工作。 Full form is something like this:完整的形式是这样的:

function UserSearchPage() {
  const [criteria, setCriteria] = useState('');
  const [searchItem, setSearchItem] = useState('');

  const [loadUsers, { loading, data }] = useLazyQuery(LoadUsersQuery);

  function PrintUsers({ data }: any) {
    return (
      <div>
        {data &&
          data.users.nodes &&
          data.users.nodes.map((c: any, i: any) => (
            <li key={i}>
              Id: {c.id}, First Name: {c.firstName}, Last Name: {c.lastName},
              Email: {c.email}, phoneNumber: {c.phoneNumber}
            </li>
          ))}
      </div>
    );
  }

  let ShowUsers = () => {
    const where: WhereInput = {};
    if (criteria === '1') {
      loadUsers({
        variables: {
          where: where
        },
      });      
    }

    if (criteria === '2') {
      if (searchItem) {
        where.firstName_contains = searchItem;
        loadUsers({
          variables: {
            where: where
          },
        });
      }
    }
  };
  return (
.....);
}

This is how the GraphQL query itself looks like:这是 GraphQL 查询本身的样子:

interface UserFilter {
  email_contains: String;
  firstName_contains?: String;
  lastName_contains?: String;
  phoneNumber_contains?: String;
  id?: Number;
}
export const LoadUsersQuery = gql`
  query usersList($where: UserFilter) {
    users(where: $where) {
      nodes {
        id
        email
      }
      totalCount
    }
  }
`;

How else can I access the data properties/errors?我还能如何访问数据属性/错误? From the console.log, I know that this is returned:从 console.log,我知道这是返回的:

Object

__typename: "User"

email: "first@first.com"

firstName: "First"

id: 148

lastName: "User"

phoneNumber: "+49123"

But if I try to access lets say data.users.id , why do I get undefined?但是如果我尝试访问让我们说data.users.id ,为什么我会得到未定义的? How can I fix this?我怎样才能解决这个问题?

As stated in other answers, it's known problem - "useLazyQuery execution function should return a promise #3499"如其他答案所述,这是已知问题 - “useLazyQuery 执行 function 应返回 promise #3499”

Instead of代替

    loadUsers({
      variables: {
        where: where
      },
    })
    .then(({ data }: any) => {
      if (data !== null && data !== undefined) {
      }
    })

you can use onCompleted option你可以使用onCompleted选项

const [loadUsers, { loading, data }] = useLazyQuery(LoadUsersQuery, {
  onCompleted: ( data : any ) => {
    if (data !== null && data !== undefined) {
      // some action
    }
  }
});

It depends on what exactly is happening in loadUsers , but it's likely that you've forgotten to a return statement there.这取决于loadUsers中究竟发生了什么,但您可能忘记了那里的 return 语句。

If you change loadUsers to return the promise for the user's it's loading, your code should start working nicely.如果您更改loadUsers以返回正在加载的用户的 promise,您的代码应该开始正常工作。

If you look at the docs for useLazyQuery , it does not return a Promise like useMutation so they behave differently.如果您查看useLazyQuery 的文档,它不会像useMutation那样返回Promise ,因此它们的行为不同。

Instead of relying on a Promise, you must utilize the second parameters (loading, data) returned when invoking useLazyQuery .您必须利用调用 useLazyQuery 时返回的第二个参数(加载、数据),而不是依赖于useLazyQuery This is why in your edit, your code works without the .then .这就是为什么在您的编辑中,您的代码可以在没有.then的情况下工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM