Home > Blockchain >  Why do I get the error "[]" when submitting a form?
Why do I get the error "[]" when submitting a form?

Time:10-06

I am trying to create a simple form handler using express. I tried the code below for my form:

<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link href="./css/style.css" rel="stylesheet" type="text/css">
        <script src="./js/main.js"></script>
        <title>CAMPEONATO CHUTESAL</title>
    </head>
    <body>
        <div >
            <div >
                <h1>Faça login<br>E participe do nosso campeonato</h1>
                <img src="./img/soccer-animate.svg"  alt="Futebol animado">
            </div>
            <form action ='/users/login' method="post">
                <div >
                    <div >
                        <h1>LOGIN</h1>
                        <div >
                            <label for="email">E-mail</label>
                            <input type="email" name="email" id="email" onchange="validateFields()" placeholder="E-mail">
                        </div>
                        <div >
                            <label for="password">Senha</label>
                            <input type="password" name="password" id="password" onchange="validateFields()" placeholder="Senha">
                        </div>
                        <button  type="submit" id="login-button" disabled="true">Login</button>
                        <label for="register" >Não tem conta? crie uma agora!</label>
                        <button type="register"  id="login-button" onclick="register()">Registrar</button>
                    </div>
                </div>
            </form>
        </div>
    </body>
</html>

Here is my index.js

// config inicial
const express = require('express')
const mongoose = require('mongoose')
const app = express()
const path = require('path')
const bodyParser = require('body-parser')

// forma de ler JSON / middlewares
app.use(
  bodyParser.urlencoded({
    extended: true,
  }),
)

app.use(bodyParser.json())

app.use(express.static(path.join(__dirname, 'public')));

// rotas da API
const usersRoutes = require('./routes/usersRoutes')

app.use('/users', usersRoutes)

And here is my usersRoutes.js

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

// CREATE - Criacao de Dados
router.post('/register',  async (req, res) => {

    const User = await new Users({
        name : req.body.txtName,
        email: req.body.txtEmail,
        password: req.body.txtPassword
    })
    User.save(function(err){
        if(err){
            console.log(err)
        }else{
            res.redirect('/')
        }
    })
  })

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

    const email = req.body.txtEmail;
    const password = req.body.txtPassword;

    const Find = await Users.find({ email, password })
    
    res.status(200).json(Find)

})
  module.exports = router

I keep getting the error "[]" after submitting the form to login in the system. I've tried everything on the internet but the error remains, am I forgetting some function? What am I forgetting?

CodePudding user response:

Your body params are named email and password.

router.post('/login', async (req, res) => {
    const { email, password } = req.body;

    const Find = await Users.find({ email, password })
    
    res.status(200).json(Find)
});

Also, you should consider encrypting your password with a library like bcrypt.
Storing them in plain text is really bad from a security point of view.

  • Related