Node assync Function does not return data

Asked

Viewed 36 times

0

index js.:

$.ajax({
        type:'POST',
        url: 'http://localhost:3000/login',
        dataType: 'application/json',
        data: {username: username, password: password}

})
  .done(data => {
        console.log(data);
});

in my Node api:

app.post('/login',async  function(req,res){

    email = req.body.username;
    password = req.body.password;

    return await db.find(email,password);

});

in the db function:

module.exports.find = async function(email,password){
   search(email,password).then(a => {
     return a;
 });


};

function search(email,password){
  return new Promise((resolve,reject) => {
    table.find({where: {email: email, password:password}}).done(function(data,err){
        if(data != "" && data != null)
          resolve(data);
      });
  });
}

The function does not return the value pro data although I used async await to wait for the answer and only come back when I picked it up, someone can help?

1 answer

0

Assuming you’re using the express, you need to use the method send to return the data to the customer.

app.post('/login',async  function(req,res){

    email = req.body.username;
    password = req.body.password;

    const resultado = await db.find(email,password);

    res.send(resultado)
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.