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?
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.
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.