-1

I have a login form in React Js which submits user details on button click.

What I am trying to get is when user submit details an API of user data is called which returns a list of the user. Then when The code gets user list I check if the user is present in the list or not.

I am stuck after the result has come. I want to return true or false to setstate of Error but instead I get undefined.

This is the function which runs

let handleSubmit = (e) => {
    e.preventDefault();
    let result;
    result = validateLogin(formDetails.name, formDetails.password);

    console.log("------------", result); //getting undefined instead of True or false 

}

ValidateLogin:

let validateLogin = async (email, password) => {
    const response = await axios.get('https://fakestoreapi.com/users');
    if (response) {
        const k = response.data.find(user => {
            return user.email === email && user.password === password
        })
        if (k) {
  
            return true
        } else {
           
            return false
        }
    }

}
Mario Petrovic
  • 7,500
  • 14
  • 42
  • 62
Ritik Singh
  • 364
  • 3
  • 15

2 Answers2

2

validateLogin is async function, so you should make handleSubmit as async function and resolve the validateLogin to get the result.

let handleSubmit = async e => {
  e.preventDefault();
  let result = await validateLogin(formDetails.name, formDetails.password);

  console.log('------------', result); //getting undefined instead of True or false
};
michael
  • 4,053
  • 2
  • 12
  • 31
0

In Validate Login File, const response = await axios.get('https://fakestoreapi.com/users'); is not executing. And therefore there is no return from Validate Login

if(response) {

    `...`

} else{

    `return something;`

}

Try to return something with else.

manasGain
  • 85
  • 1
  • 4