`await`命令导致变量失去范围

red*_*ent 1 javascript node.js async-await

await用来从数据库中提取数据.还有一个变量secuity_oktrue在一个行,然后false在下一次.任何人都可以看到问题是什么?

注意 - 如果我注释掉这一行:let session = ...那么一切正常.

Controller.prototype.changePassword = async function(request, response) {
    let model      = request.body;
    var secuity_ok = false;
    var user       = await userService.getUserByEmail(model.email);

    if (user && this.isAuthenticatedUser(request, user.id)) {
        secuity_ok  = true;
    } else {
        let session  = await authenticationService.createSessionByEmailPassword(model.email, model.oldpassword),
        secuity_ok   = !!session;
        console.log( 'A', secuity_ok ); // true
    }
    console.log( 'B', secuity_ok ); // false

    if (!secuity_ok) {
        this.sendForbiddenError(response, {
            error: 'Cannot change password: Application safeguards are preventing this action'
        });
        return new Promise(() => {});
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

输出:

A true
B false
Run Code Online (Sandbox Code Playgroud)

输出应该是:

A true
B true
Run Code Online (Sandbox Code Playgroud)

ksh*_*ine 7

你在第一行的末尾有一个逗号:

    let session  = await authenticationService.createSessionByEmailPassword(model.email, model.oldpassword), // <--- Note comma here
    secuity_ok   = !!session;
Run Code Online (Sandbox Code Playgroud)

这是声明的secuity_ok一部分let,因此它是一个额外的内部声明secuity_ok,它只在else子句的括号内.