帆:如何在函数之外获取值

sails: how to get a value outside of the function

我正在尝试从函数中获取 count 值并希望存储在函数外部。

var count;      
client.count({
                index: 'employee',
                type: 'details',
            }, function (err, response) {
                    if(err){console.log(err)}
                    count = response.count
                    console.log(count);
    });

console.log(计数); 我想在函数之外使用这个计数。 这可能吗?

包装在函数中或使用 Promise API 来重构您的代码。

function(cb){
  var count;
  ......
  cb(count);
}

试试这样写代码:

client.count({
                index: 'employee',
                type: 'details',
            }, afterCount);
function afterCount(err, count){
    if (err) console.log(err);
    // do whatever you want with count
    // if there's too much logic, split it into functions and send count as a param to them
}