0
exports.validatePasscode = (req,res) => {
    var number = req.body.number || null;
    var passcode = parseInt(req.body.passcode) || null;

    return _users_helper.validatePasscode(number, passcode).then( () =>  {
        return _users_helper.fetchUserByPhone(number)
    }).then(user => {
        console.log("abc-abc", user); 
    }).catch(err => { 
        console.log(err);
    });
}

var fetchUserByPhone = function(number){
    return new Promise(function(resolve, reject){
        return db.ref('/phones-users/' + number).once('value').then(function(snapshot){
            if(snapshot.exists() == false){ return resolve(null); };
            return fetchUserById(snapshot.child('user_id').val());
        });
    });
};
exports.fetchUserByPhone = fetchUserByPhone;


var fetchUserById = function(id){
    return new Promise(function(resolve, reject){
        return db.ref('/users/' + id).once('value').then(function(snapshot){
            if(snapshot.exists()){
                var result = snapshot.val();
                result.id = id;
                console.log("User found result", result); //this prints
                resolve(result);
            }else{
                console.log("User not found");
                resolve(null);
            }
        });
    });
}
exports.fetchUserById = fetchUserById;

When I run this code, the User found result prints correctly. However, abc-abc is not printing. Why?

TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • How could it? You've never called `vaidatePasscode`? – Felix Jan 10 '18 at 06:51
  • Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Jan 10 '18 at 23:51
  • Well if `snapshot.exists()` does return true, you *don't `resolve`* the promise. – Bergi Jan 10 '18 at 23:52

2 Answers2

1

That's not how new promise works, fetch user by phone should be:

var fetchUserByPhone = function(number){
  return db.ref('/phones-users/' + number)
  .once('value')
  .then(function(snapshot){
    if(snapshot.exists() == false){ 
      return null; 
    };
    return  fetchUserById(snapshot.child('user_id').val());
  });
};

Fetch user by id should be:

var fetchUserById = function(id){
  return db.ref('/users/' + id).once('value')
  .then(function(snapshot){
      if(snapshot.exists()){
          var result = snapshot.val();
          result.id = id;
          console.log("User found result", result); //this prints
          return result;
      }else{
          console.log("User not found");
          return null;
      }
  });
}

You don't need to create a new promise when you are calling a function that already returns a promise like type.

HMR
  • 37,593
  • 24
  • 91
  • 160
0

The promise being returned by fetchUserByPhone is not being resolved/rejected.

Since your db methods return a promise anyway, it is not necessary to create a Promise wrapper. Possible alternative:

exports.validatePasscode = (req, res) => {
  var number = req.body.number || null;
  var passcode = parseInt(req.body.passcode) || null;

  return _users_helper.validatePasscode(number, passcode).then(() => {
    return _users_helper.fetchUserByPhone(number)
  }).then(user => {
    console.log("abc-abc", user);
  }).catch(err => {
    console.log(err);
  });
}

var fetchUserByPhone = function(number) {

  return db.ref('/phones-users/' + number).once('value').then(function(snapshot) {
    if (snapshot.exists() == false) {
      return Promise.reject(null);
    };
    return fetchUserById(snapshot.child('user_id').val());
  });
};
exports.fetchUserByPhone = fetchUserByPhone;


var fetchUserById = function(id) {
  return db.ref('/users/' + id).once('value').then(function(snapshot) {
    if (snapshot.exists()) {
      var result = snapshot.val();
      result.id = id;
      console.log("User found result", result); //this prints
      return result;
    } else {
      console.log("User not found");
      return Promise.reject(null);
    }
  });
}
exports.fetchUserById = fetchUserById;
Chirag Ravindra
  • 4,760
  • 1
  • 24
  • 35
  • You don't actually need to return Promise.reject. You're already in a promise resolve handler and can just return null – HMR Jan 10 '18 at 06:59
  • @HMR, I felt that in this case, it is better to reject with an error instead of resolving with a null – Chirag Ravindra Jan 10 '18 at 07:06