[英]How to return from main function via a nested function in nodejs using callbacks?
I am trying to write a validation function, that validates my request headers. 我正在尝试编写验证函数,验证我的请求标头。 It returns true if all headers are ok and false if there is something wrong.
如果所有标题都正常则返回true,如果出现错误则返回false。 I execute this function for every (almost) request.
我为每个(几乎)请求执行此函数。 The problem is, I don't know how to return the main function in case I use callbacks and setting any flags does not work, due to some issues with variable scope.
问题是,我不知道如何返回main函数,以防我使用回调并设置任何标志不起作用,因为变量范围存在一些问题。 Everything was good when I was working without callbacks, I just used underscore to query my JSONs.
当我在没有回调的情况下工作时,一切都很好,我只是使用下划线来查询我的JSON。 Now I use NeDB and bound to callbacks I cannot get the job done.
现在我使用NeDB并绑定回调我无法完成工作。 I tried to use a global "res" variable but the problem is, when I assign the value of parameter "cnt" (0 if token not found, 1 if there is a token) to "res", then the value of "res" is always 1 iteration behind "cnt": ie:
我试图使用全局“res”变量,但问题是,当我将参数“cnt”的值(如果没有找到令牌时为0,如果有令牌则为1)分配给“res”时,则为“res”的值“总是在”cnt“后面一次迭代:即:
request1 (valid): cnt = 1; res = undefined;
request2 (valid): cnt = 1; res = 1;
request3 (invalid): cnt = 0; res = 1;
request4 (valid): cnt = 1; res = 0;
All I want to do is to return the main function with true if "cnt" = 1 and false if "cnt" = 0, either with help of a global variable or using another method. 我想要做的就是返回main函数,如果“cnt”= 1则返回true,如果“cnt”= 0则返回false,或者使用全局变量或使用其他方法。
function validateHeaders(request) {
if (request.headers.username && request.headers.deviceid) {
if (...) {
function getResult(callback) {
db.tokens.count({...
}, function (err, cnt) {
if (err) {
console.log(err);
} else {
callback(cnt);
}
});
}
getResult(function (cnt) {
res = cnt;
console.log({
count: cnt
});
});
console.log({
result: res
});
} else {
return false;
}
} else {
return false;
}
}
You can't return a meaningful value like that from a function that performs asynchronous, non-blocking operations inside. 您无法从内部执行异步,非阻塞操作的函数返回有意义的值。 What you need to do instead is pass in a callback:
你需要做的是传递一个回调:
function validateHeaders(request, cb) {
if (request.headers.username && request.headers.deviceid) {
if (...) {
db.tokens.count({
// ...
}, function (err, cnt) {
if (err) {
cb(err);
} else {
cb(null, cnt === 1);
}
});
return;
}
}
cb(null, false);
}
Then use it like: 然后使用它像:
validateHeaders(req, function(err, success) {
if (err) throw err; // TODO: improve error handling
console.log(success);
});
if you want to use the return value of callback function of nodejs. 如果要使用nodejs的回调函数的返回值。 You can't use it in sync style.
您无法在同步样式中使用它。 you can obey convention of nodejs.
你可以遵守nodejs的惯例。
function validateHeaders(request, cb) {
if (request.headers.username && request.headers.deviceid) {
if (...) {
function getResult(callback) {
db.tokens.count({...
}, function (err, cnt) {
callback(err, cnt);
});
}
getResult(function (err, cnt) {
if (err) {
cb(err);
} else {
if (cnt === 0)
cb(null, false);
else
cb(null, true);
}
});
} else {
cb(null, false)
}
}
you can use the callback result. 你可以使用回调结果。 Notice: do not use return in async function.
注意:不要在异步函数中使用return。 use callback to transfer the value.
使用回调来传输值。 the callback function of nodejs style has 2 arguments.
nodejs样式的回调函数有2个参数。 The first is err, the second is result.
第一个是错误,第二个是结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.