我有一个foo发出Ajax请求的函数.我怎样才能从中回复foo?
我尝试从success回调中返回值,并将响应分配给函数内部的局部变量并返回该变量,但这些方法都没有实际返回响应.
function foo() {
var result;
$.ajax({
url: '...',
success: function(response) {
result = response;
// return response; // <- I tried that one as well
}
});
return result;
}
var result = foo(); // It always ends up being `undefined`.
Run Code Online (Sandbox Code Playgroud) 我试图通过Node.js与MongoDB接口,并且在count()方法上遇到了一些麻烦.我正在使用node-mongodb-native,它看起来像我正在做的应该工作.我的代码示例:
var get_total_num_docs = function(db_client, query, cb){
db_client.collection(query['collection'], function(e, coll) {
coll.find(query.params, query.options, function (e, cursor) {
cursor.count(function (e, count) {
console.log(count);
return cb(e, count);
});
});
});
};
Run Code Online (Sandbox Code Playgroud)
我确信一切都存在(aka coll和cursor都定义了),但它只有在我的query.params字段为空(即查找整个集合的计数)时才有效.因此,如果我尝试使用任何类型的选择器运行查找,查找工作,但它拒绝指望返回的光标.从我在网上看到的,这看起来是正确的方法,但显然有些不对劲.感谢您的帮助!