Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to login to a page using mongoDB post request isnt recognized?

Hi there im having difficulty with my post request for login,

I’ve managed to get register working and i redirect it to login so i can attempt, the account registered goes onto mongodb correctly, from some testing with multiple logs, turns out the post for login isnt effecting it, and it simply redirects again to login, unsure how that is

heres what i got

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const mongoose = require('mongoose')


const userSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
}
 })

module.exports = mongoose.model('users', userSchema)

This is for where im storing the users info for the login page

<h1>Login</h1>

<form action="/login" method="GET">
<div>
<label for="name">Name</label>
<input type="name" id="name" name="name" required>
</div>
<div>
<label for="email">Email</label>
<input type="email" id="email" name="email" required>
</div>
<div>
<label for="password">Password</label>
<input type="password" id="password" name="password" required>
</div>
<button type="submit">Login</button>
</form>
<a href="/">Register a User</a>

Register is the same as this only with the href relating to this login page

const express = require('express')
const router = express.Router()
const users = require('../models/users')
const book = require('../models/book')

router.get('/home', async (req, res) => {
let books
try {
books = await book.find().sort({ createdAt: 'desc' }).limit(10).exec()
} catch {
books = []
}
res.render('home.ejs', { books: books })
})



//Login user
router.get('/', (req, res) => {
res.render('index.ejs')

}) 


router.get('/login', async (req, res) => {
res.render('login')

})

router.post('/login', async (req, res) => {
console.log("HI")
try{  
const valid=await users.findOne({name:req.body.name})
if (valid.password===req.body.password){
  res.render('home')

}
else{
  console.log("HI")
  res.send("Incorrect Password Try again?")
  res.render('home')
}
}catch{
console.log("HI")
res.render('home')
}
})


//Register and upload to DB 
router.post('/', async (req, res) => {

const user = ({
name: req.body.name,
email: req.body.email,
password: req.body.password,


})
await users.insertMany([user])

res.redirect('/')
})




module.exports = router

>Solution :

In your form you have mentioned method as "GET" . It is a post request so method should be "POST".

<form action="/login" method="post">
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading