0

I try to login. Well, when I consult the database to validate serch data, I request data in the matrix.

Code login.controller.ts

export async function postLogin(req: Request, res: Response): Promise<any>
{

    const conn = await connect();
    const user = await conn.query('SELECT Nombre from Usuario Where Nombre= ?', [req.body.Nombre])

    if(!user[0]) 
    {
        return res.status(400).json('Usuario o Contraseña Invalidos');
    }
    else {
        console.log('funciona');
    }
    return res.json('login');
}

I do not know how to validate that if the information is correct enter "works" and if you do not send the message 400.ja

ardoYep
  • 107
  • 3
  • 15
  • Does this answer your question? [Check if array is empty or does not exist. JS](https://stackoverflow.com/questions/24403732/check-if-array-is-empty-or-does-not-exist-js) – Rami Loiferman Dec 08 '19 at 22:34

1 Answers1

0

If I understand correctly, you're checking that a Usuario exists for the specified Nombre, in which case revising your conditional logic to the following should achieve what you require:

export async function postLogin(req: Request, res: Response): Promise<any>
{

    const conn = await connect();
    const users = await conn.query('SELECT Nombre from Usuario Where Nombre= ?', [req.body.Nombre])

    // Examine length property of users to see if any users found for
    // Nombre. If no matches found, return HTTP 400
    if(users.length === 0) 
    {
        return res.status(400).json('Usuario o Contraseña Invalidos');
    }
    else {
        console.log('funciona');
        return res.json('login');
    }
}
Dacre Denny
  • 29,664
  • 5
  • 45
  • 65