Tuesday 6 June 2017

Promise then and catch in nodeJS

Hello im having a problem with my nodejs server.

What i want its if an email exist in my db i want to send an email to that adress but if not i want to send and error message to the client.

I got this controller:

reset(req, res) {
   return  this.service.getByEmail(req.body.email)
      .then(this.mapper.outputEmail.bind(this.mapper))
      .then(res.json.bind(res)).catch(res.send.bind(res));
  }

i send and email in a post method, i use a service that call my db looking for that email and i have this mapper:

 outputEmail(outputEmail){
      return {
      email: outputEmail.email
    };
  }

My service:

getByEmail(correo) {
    return this.db.find({
      where: { email: correo }
    });
  }

My problem is that i dont know how to control if i get an empty json. If i send a post with an email that it doesnt exist i get a '{}' response, but if i send an exist email i get {email: erwrr@gmail.com}.

What i want its that if i get an empty json i just want to return an error saying that email doesnt exist, but if the query in the bd gets a real email i want to continue executing another method that its a method to send an email to that address.



via Tibo

No comments:

Post a Comment