javascript - TypeError:无法设置未定义的属性“数据”
问题描述
我目前正在做一个注册页面,使用 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);
}
这些是代码。
谢谢 ...
解决方案
推荐阅读
- pandas - 为什么我的价格列显示在电子倍数中?
- excel - 字段为0时如何不在Excel中计算
- oop - 单体架构和 SOA 的区别
- javascript - 如果一个承诺可以被拒绝,那么我们应该如何处理它,或者甚至不处理它或抓住它?
- c++ - 包含范围时如何在 int 函数中返回值?
- rust - 如何防止 Rust 程序释放()它不拥有的 C 字节
- soap - 空手道 - 我们如何查看从文件调用功能文件中的请求时发送的确切请求和输入参数
- python - XGBoost 训练时间似乎太长了
- django - Django 模型中的 DRY 字段
- java - 无法从java中的密钥库加载私钥