我目前正在做一个注册页面,使用 react.js 并使用 useMutation 使用 Axios 调用 API。用户可以注册,但显示[注册失败! TypeError: Cannot set property 'data' of undefined ] 但注册后应该会提示成功信息。如何解决这个错误。
const { Axios } = useContext(ApiContext);
const adduser = ({data}) => {
Axios
.post("/Account/Register", {
email: data.email,
fullname: data.fullname,
username: data.username,
password: data.password,
});
}
const [visible,setVisible] = useState();
const [loading, setLoading] = useState(false)
const [message, setMessage] = useState();
const history = useHistory();
const [data, setData] = useState({
email: "",
fullname: "",
username: "",
password: "",
});
const {mutate} = useMutation((adduser),
{
onSuccess: (res) => {
setData({
email: data.email,
fullname: data.fullname,
username: data.username,
password: data.password,
});
setMessage({
data: "Registered successfully.",
type: "alert-success",
});
setTimeout(() => {
res.data = setLoading(true);
history.push("/login");
}, 3000);
},
onError: (error) => {
alert(`Registered failed! ${error}.`);
}
});
const onAddUser = async e => {
e.preventDefault()
await mutate({ data})
}
const handleSubmit = async e => {
const newdata = { ...data };
newdata[e.target.id] = e.target.value;
setData(newdata);
}
这些是代码。
谢谢...