Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js NodeJS express,如何返回使用express post调用的函数的结果_Node.js_Express_Asynchronous_Async Await - Fatal编程技术网

Node.js NodeJS express,如何返回使用express post调用的函数的结果

Node.js NodeJS express,如何返回使用express post调用的函数的结果,node.js,express,asynchronous,async-await,Node.js,Express,Asynchronous,Async Await,我找到了一些如何使用wait返回值的示例,但我的异步函数中有一个函数,没有找到如何在ad.authenticate中从auth传递结果的解决方案。以下是我所拥有的: 异步函数授权(用户名、密码){ ad.authenticate(用户名、密码、函数(err、auth){ console.log(“auth:+auth”); }); } router.post('/ldapauth',函数(req,res){ var username=req.body.username; var password

我找到了一些如何使用wait返回值的示例,但我的异步函数中有一个函数,没有找到如何在ad.authenticate中从auth传递结果的解决方案。以下是我所拥有的:

异步函数授权(用户名、密码){
ad.authenticate(用户名、密码、函数(err、auth){
console.log(“auth:+auth”);
});
}
router.post('/ldapauth',函数(req,res){
var username=req.body.username;
var password=req.body.password;
授权(用户名、密码)
。然后(结果=>{
日志(“结果:+result”)
资源状态(200)。发送({
结果:结果,,
});
}).catch(错误=>{
控制台日志(err);
})
});
router.post中的结果未定义,因为我相信它是在auth函数有机会返回auth值之前调用的

Console:
    result: undefined
    auth: true

如果您想使用
。那么()
等待
您必须提示授权功能。虽然用
async
关键字标记的函数确实返回承诺,但它不会自动提示回调

您需要做的是:

function authorize(username, password){ // note: no async keyword
    return new Promise((resolve, reject) => {
        ad.authenticate(username, password, function(err, auth){
            if (err) {
                reject(err)
            }
            else {
                resolve(auth);
            }
        });
    });
}
代码的其余部分也可以。没有什么需要改变的