I'm getting this error:
Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'value')
I need to get the values from the login form (inputs: username and password) but when I submit the form the error appears.
AuthContext.js
let loginUser = async (e) => {
e.preventDefault();
let response = await fetch(
"https://estudio-ghibli-2022.herokuapp.com/login",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
username: e.target.username.value,
password: e.target.password.value,
}),
}
);
let data = await response.json();
console.log("data: ", data);
};
let contextData = {
loginUser: loginUser,
};
This is my login page:
const Login = () => {
const { loginUser } = useContext(AuthContext);
return (
<FormInner>
<Title>Studio Ghibli Tracker</Title>
<Subtitle>Login</Subtitle>
<Container>
<label>Email</label>
<input name="username" placeholder="example@mail.com" />
<img src={userLogo} alt="user logo" />
</Container>
<Container>
<label>Password</label>
<input name="password" placeholder="******" />
<img src={key} alt="key logo" />
</Container>
<LoginBtn type="button" onClick={loginUser}>
Login
</LoginBtn>
</FormInner>
);
};