Javascript 具有嵌套回调函数的函数不返回值
我有一个函数,其中有一个嵌套的回调函数结构。我希望第一个“母亲”函数在回调函数序列中计算后返回某个值。然而。这真的不管用。这是代码的简化版本,你认为你能帮上忙吗Javascript 具有嵌套回调函数的函数不返回值,javascript,node.js,callback,Javascript,Node.js,Callback,我有一个函数,其中有一个嵌套的回调函数结构。我希望第一个“母亲”函数在回调函数序列中计算后返回某个值。然而。这真的不管用。这是代码的简化版本,你认为你能帮上忙吗 console.log(finalResult()); function finalResult() { var finalanswer = firstFunction(secondFunction); function firstFunction (callback) { var notion = 1;
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;
}
返回getContext(makeQuery);
函数getContexts(回调){
cypherQuery(上下文查询,函数(err,cypherAnswer){
如果(错误){
err.type='neo4j';
返回回调(err);
}
//无错误?将上下文传递给makeQuery函数
返回回调(null,cypherAnswer);
});
}
函数makeQuery(err,answer){
//错误?显示它。
if(err)console.log(err);
//定义存储新上下文的位置
var newcontexts=[];
//这是一个数组,用于检查数据库中是否存在不存在的上下文
var检查=[];
//检查我们从DB收到的所有上下文,并从中创建newcontexts变量
对于(var i=0;i您正在将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;
}
//完成后使用回调调用GetContext
getContexts(函数(错误,上下文){
控制台日志(err);
console.log(上下文);
});
函数getContexts(回调){
cypherQuery(上下文查询,函数(err,cypherAnswer){
如果(错误){
err.type='neo4j';
返回回调(err);
}
//无错误?将上下文传递给makeQuery函数
var contexts=makeQuery(null,cypherAnswer);
//我们有我们的答案,打电话回拨
回调(空,上下文);
});
}
函数makeQuery(err,answer){
//错误?显示它。
if(err)console.log(err);
//定义存储新上下文的位置
var newcontexts=[];
//这是一个数组,用于检查数据库中是否存在不存在的上下文
var检查=[];
//检查我们从DB收到的所有上下文,并从中创建newcontexts变量
对于(var i=0;idid您是否尝试返回回调(null,concept)
?请发布完整的代码..您在哪里定义secondFunction
以及如何调用finalResult()
抱歉,我在简化时输入了一个错误。事实上,makeQuery=SecondFunction我刚刚与JavaScript开发人员的精神交谈了很久,他们告诉我你正试图从一个异步函数返回。我的精神力量正确吗?仅供参考,你的代码。只要代码不进行任何异步调用,这将起作用。代码在原始问题不进行异步调用,因此您是正确的,但是@deemeetree应该知道,一旦代码进行异步调用,这将不起作用。奇怪的是,它对我不起作用……问题中的代码对您不起作用,或者您没有向我们展示的代码对您不起作用?根据您发布的代码,我的答案是:“是的。如果您需要有关不起作用的代码的帮助,我建议您发布它。@Andrew原始代码在上面-我添加了它。看起来我的代码不起作用,因为第二个函数的返回嵌套在数据库查询函数中,可能它的返回不是函数的返回……但我不知道如何解决它。”。..检查我更新的答案。正如其他人已经指出的,您异步调用了一个函数,并试图从该函数中返回一个值。这里更好的方法是将匿名函数传递到GetContext,异步函数完成后将调用该函数。然后,您可以按图所示记录您的答案。