我有一个功能,并且信息未定义。
var info = queryCollection(t,where,type,function(result){
var rek=result;
return rek;
});
代码在服务器端的节点js中使用mongo db
答案 0 :(得分:0)
你基本上是在询问javascript的核心概念之一,异步性如何。这只是这个平台的范围之外,我强烈建议在继续你想要构建的任何内容之前查看一些教程。这个说法我已经设置了一个小例子。
https://jsfiddle.net/xavf01rn/
var myAsyncFunction = function(callback) {
console.log("Now in async function...");
// set timeout is an function that is async. E.g. like your queryCollection fundtion
setTimeout(function(){
console.log("Passing back the result to the callback!");
callback("dummyResultValue");
}, 0)
console.log("Now exiting async function...");
};
// note how we pass in a function as parameter. It will be called once the async action has completed
var result = myAsyncFunction(function(asyncResult) {
console.log("Got result in async function callback:", asyncResult); // asyncResult has a value
});
console.log("Result right after calling the myAsyncFunction function:", result); // result = undefined
输出看起来像这样,很可能与您的预期不同。这是由于JS的异步性质。
Now in async function...
Now exiting async function...
Result right after calling the myAsyncFunction function: undefined
Passing back the result to the callback!
Got result in async function callback: dummyResultValue
尝试理解为什么输出看起来像。一旦你理解了这一点:耶!您知道熟悉了使语言如此强大的最重要的JS概念之一(以及其他原因)。