gpt4 book ai didi

reactjs - useMutation 中的 onCompleted 回调没有返回值

转载 作者:行者123 更新时间:2023-12-03 23:09:25 25 4
gpt4 key购买 nike

我正在尝试使用 apollo 在 react native 中实现登录。
在 native 应用程序中 react

const SIGN_IN = gql`
mutation($username: String!, $password: String!) {
signin(password: $password, username: $username) {
user {
username
}
token
}
}
`;

//代码被缩写。
function LoginScreen() {
const [signIn, { loading, error }] = useMutation(SIGN_IN, {
onCompleted({ data }) {
if (loading) console.log("Loading.....");
console.log("Printing data");
console.log(data.signin.token);
}
});
}

后端服务器运行良好。
但是我在控制台日志中收到一个错误说
    [Unhandled promise rejection: TypeError: Cannot read property 'signin' of undefined]

Stack trace:
screens/LogInScreen.js:36:6 in useMutation$argument_1.onCompleted
node_modules/@apollo/react-hooks/lib/react-hooks.cjs.js:635:25 in callOncomplete

数据未定义。
所以我尝试了 { data && console.log(data.signin.token) }但它什么也不打印。
我读过文档说“对 useMutation 的 onCompleted 回调,一旦突变完成其返回值就会被调用。”

我该如何调试?我错过了什么?有任何想法吗?
谢谢!

最佳答案

onCompleted将整个数据对象作为其第一个参数传递,因此您的签名应如下所示:

onCompleted(data)

或使用解构
onCompleted({ signin })

关于reactjs - useMutation 中的 onCompleted 回调没有返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59650831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com