使用 apollo/client - graphql 时出现奇怪的错误
2021-04-18
865
我正在开发一个 MERNG 应用程序,到目前为止一切都运行良好,但我在 useMutations 的前端遇到了一些错误,让我向您解释一下。
因此,在登录/注册部分,如果您将字段留空或在登录中,如果在数据库中找不到您的用户,它会出现错误,并且我在函数 onError(err) 中收到这些错误,但是,问题是,它总是给我错误,它说,无法读取未定义的属性“扩展”,即使我没有错误,它也会给出错误,让我给你展示代码
import React, { useState } from "react";
import { useMutation, gql } from "@apollo/client";
import { useHistory } from "react-router-dom";
import { useDispatch } from "react-redux";
import {
FormAuth,
TitleAuth,
TitleAuthImage,
ContainInput,
InputAuth,
ContainSingleInput,
ContainButtons,
Button,
ButtonSpan
} from "../../website/Auth/AuthStyled";
import instagram from "../../img/instagram.svg";
import Loading from "../Loading/Loading";
const Login = ({ setLogin }) => {
const history = useHistory();
const dispatch = useDispatch();
const [userData, setUserData] = useState({
email: "",
password: ""
});
const [errors, setErrors] = useState({});
const [addUser, { loading }] = useMutation(LOGIN_MUTATION, {
update(
_,
{
data: { login: data }
}
) {
dispatch({ type: "LOGIN", payload: data });
history.push(`/profile/${data.id}`);
},
onError(err) {
setErrors(err.graphQLErrors[0].extensions.exception.errors);
},
variables: userData
});
const handleSubmit = e => {
e.preventDefault();
addUser();
setUserData({ email: "", password: "" });
};
// Framer motion animations
const PopUp = {
hidden: { x: 1000 },
visible: {
x: 0,
transition: { duration: 0.7 }
}
};
return (
<>
{!loading ? (
<FormAuth
onSubmit={handleSubmit}
variants={PopUp}
initial="hidden"
animate="visible"
>
<TitleAuth>
<TitleAuthImage src={instagram} />
</TitleAuth>
<ContainInput>
<ContainSingleInput top="2rem">
<InputAuth
name="email"
type="email"
placeholder={errors.email ? errors.email : "Email"}
maxLength="50"
value={userData.email}
onChange={e =>
setUserData({ ...userData, email: e.target.value })
}
/>
</ContainSingleInput>
<ContainSingleInput top="7rem">
<InputAuth
name="password"
type="password"
placeholder={errors.password ? errors.password : "Password"}
maxLength="50"
value={userData.password}
onChange={e =>
setUserData({ ...userData, password: e.target.value })
}
/>
</ContainSingleInput>
</ContainInput>
<ContainButtons>
<Button
type="submit"
Login
whileHover={{ scale: 1.1 }}
whileTap={{ scale: 0.85 }}
>
Login
</Button>
<ButtonSpan
Register
whileHover={{ scale: 1.1 }}
whileTap={{ scale: 0.85 }}
onClick={() => setLogin(false)}
>
Register
</ButtonSpan>
</ContainButtons>
</FormAuth>
) : (
<Loading />
)}
</>
);
};
const LOGIN_MUTATION = gql`
mutation login($email: String!, $password: String!) {
login(email: $email, password: $password) {
id
email
password
token
}
}
`;
export default Login;
单击登录后浏览器中的错误
这里奇怪的是,它之前正常工作,但知道它坏了,我不知道为什么或如何,如果我遇到错误,它们将出现在输入的占位符中,但知道,它总是坏掉
1个回答
根据阿波罗文档 onerror for Usemuty 通过<a href="https://github.com/apollographql/apollographql/apollo-clob/blob/d96f4578f89b933333393950395035a39503f6cdb59eee8/src/errors/src/errors/errors/errors/Index.tss.tss.tssul36-l36-l36-l36-lorerror <nof.包含几个元素。</p>
在您的当前实现中,您假设
graphqlerrors
element
onerror
,并且确实与至少填充了它1个元素。可能并非如此。因此,当您尝试访问
err.graphqlerrors [0]
时,您获得
undefined
会导致您的特定错误。
258625865
另外,您可以使用 跟踪加载和错误状态
761625854
Henry Ecker
2021-04-18