I'm trying to build a web app with a login and register form. But when registration fails every time I register. here is the post method for register page:
app.post('/signup', async (req, res) => {
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10);
users.push({
id: Date.now.toString(),
name: req.body.name,
mail: req.body.mail,
username: req.body.username,
contact: req.body.contact,
password: hashedPassword,
});
res.redirect('/signin');
} catch{
res.redirect('/signup');
console.log("some error occured, try again!!");
}console.log(users);
});
this is the snippet of the ejs file's form section
<form class="flex flex-wrap -m-2" action="/signup" method="POST">
<!-- Email input -->
<div class="p-2 w-full">
<input class="w-full rounded border border-white text-white focus:outline-none focus:border-red-700 text-base px-4 py-2" placeholder="Email address" type="email" name="mail" required>
</div>
<!-- Full Name input -->
<div class="p-2 w-full">
<input class="w-full rounded border border-white text-white focus:outline-none focus:border-red-700 text-base px-4 py-2" placeholder="Full Name" type="text" name="name" required>
</div>
<!-- Username input -->
<div class="p-2 w-full">
<input class="w-full rounded border border-white text-white focus:outline-none focus:border-red-700 text-base px-4 py-2" placeholder="Username" type="text" name="username" required>
</div>
<!-- Phone Number input -->
<div class="p-2 w-full">
<input class="w-full rounded border border-white text-white focus:outline-none focus:border-red-700 text-base px-4 py-2" placeholder="Contact Number" type="text" name="contact">
</div>
<!-- Password input -->
<div class="p-2 w-full">
<input class="w-full rounded border border-white text-white focus:outline-none focus:border-red-700 text-base px-4 py-2" placeholder="Password" type="password" name="password" required>
</div>
<!-- Confirm Password input -->
<div class="p-2 w-full">
<input class="w-full rounded border border-white text-white focus:outline-none focus:border-red-700 text-base px-4 py-2" placeholder="Confirm Password" type="password" name="confirm_password" required>
</div>
<!-- register Button -->
<div class="p-2 w-full">
<button class="flex mx-auto text-white bg-black border-0 py-2 px-8 focus:outline-none hover:bg-gray-900 rounded text-lg">sign Up</button>
</div>
</form>
Every time I run the server, the try block didn't execute. The structure of the project is as follows:
It's impossible to know what your error is because you're not capturing it. Write your catch block like this:
app.post('/signup', async (req, res) => {
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10);
users.push({
// Date.now.toString() => "function now() { [native code] }"
// I think you mean Date.now()
id: Date.now().toString(),
name: req.body.name,
mail: req.body.mail,
username: req.body.username,
contact: req.body.contact,
password: hashedPassword,
});
res.redirect('/signin');
} catch (error) {
res.redirect('/signup');
console.log(error); // What is output here?
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.