【问题标题】:A function that has nested callback functions does not return values [duplicate]具有嵌套回调函数的函数不返回值[重复]
【发布时间】:2014-05-28 10:37:38
【问题描述】:

我有一个函数,其中有一个嵌套的回调函数结构。我希望第一个“母亲”函数在回调函数序列中计算后返回某个值。然而。它真的不起作用。这是代码的简化版本,您认为您可以帮忙吗?

console.log(finalResult());

function finalResult() {    

var finalanswer = firstFunction(secondFunction);

function firstFunction (callback) {
    var notion = 1;
    callback(null, notion);
}

function secondFunction (err, notion) {
    if (err) console.log(err);
    var answer = notion + 1
    return answer;
}

return finalanswer;  

}  

谢谢!

**更新 - 原始代码**

 return getContexts(makeQuery);


 function getContexts (callback) {

    dbneo.cypherQuery(context_query, function(err, cypherAnswer){

        if(err) {
            err.type = 'neo4j';
            return callback(err);
        }
        // No error? Pass the contexts to makeQuery function
        return callback(null,cypherAnswer);



    });




}

function makeQuery (err,answer) {
    // Error? Display it.
    if (err) console.log(err);

    // Define where we store the new contexts
    var newcontexts = [];

    // This is an array to check if there are any contexts that were not in DB
    var check = [];

    // Go through all the contexts we received from DB and create the newcontexts variable from them
    for (var i=0;i<answer.data.length;i++) {
        newcontexts.push({
            uid: answer.data[i].uid,
            name: answer.data[i].name
        });
        check.push(answer.data[i].name);
    }

    // Now let's check if there are any contexts that were not in the DB, we add them with a unique ID
    contexts.forEach(function(element){
        if (check.indexOf(element) < 0) {
            newcontexts.push({
                uid: uuid.v1(),
                name: element
            });
        }
    });

    return newcontexts;

}

【问题讨论】:

  • 你试过return callback(null, notion) 吗?
  • 请发布完整代码..你在哪里定义secondFunction,你怎么称呼finalResult()
  • 对不起,我在简化时打错了。实际上 makeQuery = secondFunction
  • 我刚刚谈到了早已逝去的 JavaScript 开发人员的精神,他们告诉我您正试图从异步函数返回。我的精神力量是否到位?

标签: javascript node.js callback


【解决方案1】:

您将finalanswer 设置为等于firstFunction 的返回值,但是,如果您查看firstFunction 的主体,它不会返回任何内容。 firstFunction 的最后一行应该是:

return callback(null, notion);

我在 Chrome 控制台中快速测试了它,它似乎按预期工作,并将2 记录到控制台。

更新

既然原始代码已经发布,我将更新代码:

// call getContexts with a callback when complete
getContexts(function(err, contexts){
    console.log(err);
    console.log(contexts);
});

function getContexts (callback) {
    dbneo.cypherQuery(context_query, function(err, cypherAnswer){
        if(err) {
            err.type = 'neo4j';
            return callback(err);
        }
        // No error? Pass the contexts to makeQuery function
        var contexts = makeQuery(null,cypherAnswer);

        // we have our answer, call the callback
        callback(null, contexts);
    });
}

function makeQuery (err,answer) {
    // Error? Display it.
    if (err) console.log(err);

    // Define where we store the new contexts
    var newcontexts = [];

    // This is an array to check if there are any contexts that were not in DB
    var check = [];

    // Go through all the contexts we received from DB and create the newcontexts variable from them
    for (var i=0;i<answer.data.length;i++) {
        newcontexts.push({
            uid: answer.data[i].uid,
            name: answer.data[i].name
        });
        check.push(answer.data[i].name);
    }

    // Now let's check if there are any contexts that were not in the DB, we add them with a unique ID
    contexts.forEach(function(element){
        if (check.indexOf(element) < 0) {
            newcontexts.push({
                uid: uuid.v1(),
                name: element
            });
        }
    });

    return newcontexts;
}

【讨论】:

  • 只要代码不进行任何异步调用,这将起作用。原始问题中的代码不会进行异步调用,因此您是正确的,但@deemeetree 应该知道,一旦代码进行异步调用,这将不起作用。
  • 奇怪,它对我不起作用...
  • 问题中的代码对您不起作用,或者您未向我们展示的代码对您不起作用?根据您发布的代码,我的回答是正确的。如果您需要有关不起作用的代码的帮助,我建议您发布。
  • @Andrew 原始代码在上面-我添加了它。看起来我的代码不起作用,因为第二个函数的返回嵌套在数据库查询函数中,并且它的返回可能不是函数的返回......但我不知道如何解决它......跨度>
  • 检查我更新的答案。正如其他人已经指出的那样,您异步调用了一个函数并尝试从该函数中返回一个值。这里更好的方法是将一个匿名函数传递给 getContexts,该函数将在异步函数完成时被调用。然后你可以如图所示记录你的答案。
猜你喜欢
  • 2015-03-15
  • 2015-08-01
  • 2014-09-01
  • 2015-07-20
  • 1970-01-01
  • 2016-08-14
  • 2013-08-13
  • 2011-03-23
相关资源
最近更新 更多