I’m making a sign-up page, and can’t seem to come across why it’s not working.
Edit: updated the code to remove the userObject. Still am not able to get the code to actually post the data to my json file.
New code:
import { useState } from "react";
const Signup = () => {
const [formData, setFormData] = useState({
email: "",
password: "",
confirmPassword: "",
});
const handleInputChange = (event) => {
setFormData({
...formData,
[event.target.name]: event.target.value,
});
};
console.log(formData)
const handleSubmit = (event) => {
event.preventDefault();
console.log(formData);
fetch("http://localhost:8000/users", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(formData),
}).then(() => {
window.location.href = "/profile";
});
};
return (
<div>
<h2>Sign up</h2>
<form onSubmit={handleSubmit}>
<div>
<label>Email:</label>
<input
type="email"
name="email"
value={formData.email}
onChange={handleInputChange}
required
/>
</div>
<div>
<label>Password:</label>
<input
type="password"
name="password"
value={formData.password}
onChange={handleInputChange}
required
/>
</div>
<div>
<label>Confirm Password:</label>
<input
type="password"
name="confirmPassword"
value={formData.confirmPassword}
onChange={handleInputChange}
required
/>
</div>
<button type="submit">Sign up</button>
</form>
</div>
);
};
export default Signup;
Old code:
import { useState } from "react";
const Signup = () => {
const [formData, setFormData] = useState({
email: "",
password: "",
confirmPassword: "",
});
const handleInputChange = (event) => {
setFormData({
...formData,
[event.target.name]: event.target.value,
});
};
console.log(formData)
const handleSubmit = (event) => {
event.preventDefault();
const userObject = {
email: formData.email,
password: formData.password,
};
fetch("http://localhost:8000/users", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(userObject),
}).then(() => {
window.location.href = "/profile";
});
};
console.log(userObject)
return (
<div>
<h2>Sign up</h2>
<form onSubmit={handleSubmit}>
<div>
<label>Email:</label>
<input
type="email"
name="email"
value={formData.email}
onChange={handleInputChange}
required
/>
</div>
<div>
<label>Password:</label>
<input
type="password"
name="password"
value={formData.password}
onChange={handleInputChange}
required
/>
</div>
<div>
<label>Confirm Password:</label>
<input
type="password"
name="confirmPassword"
value={formData.confirmPassword}
onChange={handleInputChange}
required
/>
</div>
<button type="submit">Sign up</button>
</form>
</div>
);
};
export default Signup;
When I console.log the formData, it is receiving the data, but when I try and console.log the userObject, it comes back as not defined. I’ve been Googling for hours, checked solutions to similar problems on here, and trying different methods, but can’t seem to get it to save to my json file. Hoping that with some new eyes I could get some help. Thanks!
2
Answers
This is normal. I’m sorry to tell you, but your
console.log
function is not called at the right time!Your code should be:
Like spender said:
scope error occured.
If you want to log the userObject, you can move the console.log statement inside the handleSubmit,