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

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

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 class="main-login">
            <div class="left-login">
                <h1>Faça login<br>E participe do nosso campeonato</h1>
                <img src="./img/soccer-animate.svg" class="left-login-image" alt="Futebol animado">
            </div>
            <form action ='/users/login' method="post">
                <div class="right-login">
                    <div class="card-login">
                        <h1>LOGIN</h1>
                        <div class="textfield">
                            <label for="email">E-mail</label>
                            <input type="email" name="email" id="email" onchange="validateFields()" placeholder="E-mail">
                        </div>
                        <div class="textfield">
                            <label for="password">Senha</label>
                            <input type="password" name="password" id="password" onchange="validateFields()" placeholder="Senha">
                        </div>
                        <button class="btn-login" type="submit" id="login-button" disabled="true">Login</button>
                        <label for="register" class="label2">Não tem conta? crie uma agora!</label>
                        <button type="register" class="btn-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

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 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?

>Solution :

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.

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