gpt4 book ai didi

javascript - 无法通过递归调用 node.js 中的函数从 promisese 获得响应

转载 作者:行者123 更新时间:2023-11-29 23:30:52 24 4
gpt4 key购买 nike

使用 node.js Q promises 库 我怀疑递归调用函数是否有用。我尝试了两种方法来解决我的问题。在第一种方法中,我尝试直接从递归函数返回,而在第二种方法中,我尝试使用 Q promise 库。

这是我调用该递归函数并期待此处响应的代码。

function getSearchedUserInTree(userId,body){
var deferred = Q.defer();
var loggedInUserId = userId;
var searchUserIdSql = "SELECT rc.user_id,CAST(b.node_id AS CHAR) AS node_id FROM refer_codes AS rc LEFT JOIN btrees AS b ON b.user_id=rc.user_id WHERE rc.refer_code='"+body.refer_code+"'";
connection.query(searchUserIdSql, function (err, result,field) {

var getLoggedInNode = "SELECT CAST(b.node_id AS CHAR) AS node_id FROM btrees as b WHERE b.user_id='"+loggedInUserId+"'";
connection.query(getLoggedInNode, function (err, loggedResult,field) {
helper.trackRelation(loggedResult[0].node_id,result[0].node_id,function(err,relationResult){
if(err) throw err;
deferred.resolve(relationResult);
});
});
});

return deferred.promise;
}

从“trackRelation()”函数获得返回的第一种方法。

function trackRelation(parentNode,childNode){
var deferred = Q.defer();
var childNodeId = bigInt(""+childNode+"");
var parentNodeId = bigInt(""+parentNode+"");
if(bigInt(childNodeId).compare(parentNodeId) == 0){
var childSql = "SELECT * FROM users WHERE id='"+childNodeId.toString()+"'";
connection.query(childSql,function(err,packResult,field){
console.log(packResult);
deferred.resolve({status:200,message:'success',packResult});
});
}else{
/* Detact position */
if(bigInt(childNodeId).mod(2) == 0){
var tempParentNodeId = bigInt(childNodeId).divide(2);
}else{
var subResult = bigInt(childNodeId).subtract(1);
var tempParentNodeId = subResult.divide(2);
}

/** If reached at top but parent not found **/
if(bigInt(tempParentNodeId).compare(1) == 0){
var childSql = "SELECT * FROM users WHERE id='"+tempParentNodeId.toString()+"'";
connection.query(childSql,function(err,packResult,field){
console.log(packResult);
deferred.resolve({status:200,message:'success',packResult});
});
}else{
return trackRelation(parentNodeId,tempParentNodeId.toString());
}

}
return deferred.promise;
}

从递归函数中获取返回值的第二种方法,无需 promise 直接返回值。

function trackRelation(parentNode,childNode){      
var deferred = Q.defer();
var childNodeId = bigInt(""+childNode+"");
var parentNodeId = bigInt(""+parentNode+"");
if(bigInt(childNodeId).compare(parentNodeId) == 0)
{
deferred.resolve({status:200,message:'success',topUserId:childNodeId.toString()});
return childNodeId.toString();
}else
{
/* Detact position */
if(bigInt(childNodeId).mod(2) == 0){
var tempParentNodeId = bigInt(childNodeId).divide(2);
}else
{
var subResult = bigInt(childNodeId).subtract(1);
var tempParentNodeId = subResult.divide(2);
}

/** If reached at top but parent not found **/
if(bigInt(tempParentNodeId).compare(1) == 0){
return tempParentNodeId.toString();
}else
{
return trackRelation(parentNodeId,tempParentNodeId.toString());
}
}
return deferred.promise;
}

我没有从这两种方法中得到任何回应。我试图在控制台中打印我需要的值作为响应,我得到的值与期望值相同,但无法获得该值作为响应。

最佳答案

您无法像从回调中取回那样从 promise 中取回值。

您的方法 trackRelation 返回一个 promise ,其值应该使用 .then() 方法访问。

例子:

trackRelation(args).then(function(res){
//when the promise resolves, this method is called
}, function(err){
//when the promise rejects, this method is called
});

查看 API 引用页 here .

关于javascript - 无法通过递归调用 node.js 中的函数从 promisese 获得响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47406228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com