0

I need to return true value, so I can catch it in React-Native, but when I try to connect to the account with the right values, I get undefined in the console of the back-end

login : async function (email, password) {
    try {
        const sqlEmail = "Select * from users where email = ?";
        db.query(sqlEmail, email, async (error, results) => {
            console.log(results);
            if(!results || !(await bcryptjs.compare(password, results[0].password))) {
                console.log("tes pas connecté")
                return false;                   
            }  else {
                return true;
            }
        })    
    } catch (error) {
        console.log(error);
        throw new Error (error)            
    }        
},

and this is my route :

router.post('/login', function (req, res)  {
    userController.login(req.body.email, req.body.password)
    .then((result) => {
        console.log(result)
        res.send(result)
    }).catch(err => {
        res.send(err)
    })
})

Thank you

Eldar
  • 9,781
  • 2
  • 10
  • 35
  • 1
    Your `login` function has no `return` statement. `return true` belongs to the anonymous callback function you pass to `query`. – Quentin Nov 11 '20 at 15:17
  • Why is `login` marked with the `async` keyword? You aren't `await`ing anything inside it. – Quentin Nov 11 '20 at 15:17
  • Because i'm in module.exports so I think that I have to use Async function, I'm beginner but I have to return true in the Else statement, how can I do it ? – Android Hackers Nov 11 '20 at 15:30
  • There is no requirement that a function be marked as `async` in order to export it. See the duplicate for a discussion of working with asynchronous functions that use callbacks. – Quentin Nov 11 '20 at 15:31
  • Just a sample answer above response => `login : async function (email, password) { let _resolve, _reject; const dbQueryPromise = new Promise((resolve, reject) => { _resolve = resolve; _reject = reject; }); db.query(sqlEmail, email, (error, results) => { if (error) { _reject(error); } const isMatched = await bcryptjs.compare(password, results[0].password); if(!results || !isMatched) { console.log("tes pas connecté") _resolve(true); } else { _resolve(false); } }); return await dbQueryPromise; },` – Semih Gokceoglu Nov 12 '20 at 04:52
  • Thank u for your code, but it tells me that the await before the bcrypt can not be there because its not an async function, how can I resolve it ? – Android Hackers Nov 12 '20 at 11:04
  • It's okay, I resolved the problem, thank you very much, here is the good code : – Android Hackers Nov 12 '20 at 13:03

0 Answers0