简体   繁体   English

为什么我的JavaScript承诺无法解决?

[英]Why doesn't my javascript promise resolve?

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. 当我运行此代码时, User found result正确打印。 However, abc-abc is not printing. 但是, abc-abc无法打印。 Why? 为什么?

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: 按ID提取用户应为:

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. 当您调用已经返回类型(例如类型)的promise的函数时,无需创建新promise。

The promise being returned by fetchUserByPhone is not being resolved/rejected. fetchUserByPhone返回的fetchUserByPhone未得到解决/拒绝。

Since your db methods return a promise anyway, it is not necessary to create a Promise wrapper. 由于您的db方法无论如何都返回一个Promise,因此没有必要创建Promise包装器。 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; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM