我正在使用 React 制作一个将向 MongoDB 发送 POST 的表单,当我开始尝试填写表单时,会发生以下错误:
e.preventDefault is not a function
TypeError: e.preventDefault is not a function
at handleChange (http://localhost:3000/main.451f9e824d0c94815e46.hot-update.js:96:7)
at http://localhost:3000/static/js/bundle.js:75093:9
at http://localhost:3000/static/js/bundle.js:65561:23
at http://localhost:3000/static/js/bundle.js:75185:20
at http://localhost:3000/static/js/bundle.js:65561:23
at http://localhost:3000/static/js/bundle.js:75552:5
at http://localhost:3000/static/js/bundle.js:65561:23
at http://localhost:3000/static/js/bundle.js:66024:33
at http://localhost:3000/static/js/bundle.js:64750:23
at http://localhost:3000/static/js/bundle.js:75552:5
这是preventDefault所在的代码:
const handleSubmit = async (e) => {
e.preventDefault();
const res = await fetch("http://localhost:5001/client/projects", {
method: "POST",
body: JSON.stringify(project),
headers: { "Content-Type": "application/json" },
});
const data = await res.json(project);
console.log(data);
console.log(project);
};
const handleChange = (e) => {
e.preventDefault();
setProject({ ...project, [e.target.name]: e.target.value });
};
非常感谢所有能帮助我的人:)