My POST request isn’t working and it’s not updating? The error is:
TypeError: Cannot read properties of undefined (reading 'username')
app.post('/create-user', function(req, resp) {
const client = new MongoClient(uri);
const dbo = client.db("eggyDB"); // Get the database object
const collName = dbo.collection("users"); // Get the collection
const info = {
username: req.body.username,
password: req.body.password
};
collName.insertOne(info)
.then(function() {
console.log('User created');
resp.render("main", {
layout: "homepage",
title: "My Home page"
});
})
.catch(function(err) {
console.error('Error creating user:', err);
resp.status(500).send('Internal Server Error');
})
});
this is my html code (in the partials folder):
<form
name="signup"
action="/create-user"
method="POST"
class="popup-signup-content"
onsubmit="return verifyPassword()"
>
<h1 onclick="closeSignup()" id="close-login" type="submit">x</h1>
<img src="/images/LOGO-YL.png" alt="None" />
<input
id="username-signup"
name="username"
type="text"
placeholder="Enter Username/Email"
required
/>
<input
id="password-signup"
type="password"
placeholder="Enter Password"
required
/>
<input
id="verify-password-signup"
name="password"
type="password"
placeholder="Re-Enter Password"
required
/>
<button class="login-button bold" type="submit" onclick="console.log('Form Submitted')">Register</button>
</form>
it won’t add a new user to the database but it connects to the database. it also won’t show the onclick function when Register is pressed. what should i do?
i tried asking chatgpt but i think it was slowly drifting me away from the solution. what do you think should i do here?