node assync function does not return data

0

index.js:

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

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

on my api node:

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 pro date value although I have used async await to wait for the answer and only come back when I get the same one, can anyone help?

    
asked by anonymous 22.10.2017 / 00:16

1 answer

0

Assuming you are using express , you need to use the send method to return the data to the client.

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)
});
    
23.10.2017 / 03:12