首页 > 解决方案 > 如何访问 mongoose find() 外部的变量并返回它

问题描述

我试图在里面调用一个带有 mongoose find() 的函数,然后将返回的值传递给变量 x 但该变量始终为空。我不知道为什么 a 仍然等于“”,即使它在通过 mongoose find() 后应该是另一个值。就好像它在执行 mongoose find() 之前返回了 a 的值。

有没有办法做到这一点?谢谢。

 function checkRange(vals) {

    let a="";

    exports.accountType.find({},function(err,foundAccounts) {
           //some if-else statements
          // a could be any value depending on the condition
    });

    return a;
 }


var x = checkRange("123");

标签: node.jsmongoose

解决方案


推荐阅读