为什么 next() 指令的位置会改变我的应用程序的行为?

Why the position of the next() instruction change the behavior of my application?

我按照 Keystone documentation 创建了一个新项目。 我正在尝试创建一个根据用户是否登录而表现不同的页面。

我的哈巴狗页面是这样的:

extends ../layouts/default

block content
    if(!user)
        .container: .jumbotron
            .row
                .col-md-2
                .col-md-8.text-center.welcome
                    h2 Welcome
                .col-md-2
    else
        .container: .jumbotron
            .row
                .col-md-2
                .col-md-8.text-center.welcome
                    h2 Logged in
                .col-md-2
            .row

我的 index.js 最后有这个,用于公开端点:

// Setup Route Bindings
exports = module.exports = function (app) {
    // Views
    app.get('/', routes.views.index);
    app.all('/signin', routes.views.signin);
};

服务路由 signin 端点的 JavaScript 文件是:

var keystone = require('keystone');
var User = keystone.list('User');
var bcrypt = require('bcrypt');

exports = module.exports = function (req, res) {

    var view = new keystone.View(req, res);
    var locals = res.locals;
    var utente = req.body.utente;
    var passwd = req.body.password;

    // locals.section is used to set the currently selected
    // item in the header navigation.
    locals.section = 'home';

    view.on('init', function(next){
        next();
    });

    view.on('post', { action: 'signin' }, function (next) {
        // console.log("utente: " + utente);

        User.model.find()
            .where('email', utente)
            .exec()
            .then(function (user) { //first promise fulfilled
                //return another async promise

                console.log("user: ", user);
                console.log("email: ", user[0].email);
                console.log("password: ", user[0].password);

                bcrypt.compare(passwd, user[0].password, function(err, res) {
                    if(res){
                        console.log("Signin OK!");
                        locals.user = user[0];
                    }else{
                        console.log("Wrong Email or Password!");
                        req.flash("error", "wrong email or password!");
                    }
                });

            }, function (err) { //something happened
                //catch the error, it can be thrown by any promise in the chain
                console.log(err);
            });

        next();
    });

    // Render the view
    view.render('index');
};

但我的哈巴狗页面无法识别 user 变量,应该是这样。

相反,如果我在内部移动 next() 指令:

var keystone = require('keystone');
var User = keystone.list('User');
var bcrypt = require('bcrypt');

exports = module.exports = function (req, res) {

    var view = new keystone.View(req, res);
    var locals = res.locals;
    var utente = req.body.utente;
    var passwd = req.body.password;

    // locals.section is used to set the currently selected
    // item in the header navigation.
    locals.section = 'home';

    view.on('init', function(next){
        next();
    });

    view.on('post', { action: 'signin' }, function (next) {
        // console.log("utente: " + utente);

        User.model.find()
            .where('email', utente)
            .exec()
            .then(function (user) { //first promise fulfilled
                //return another async promise

                console.log("user: ", user);
                console.log("email: ", user[0].email);
                console.log("password: ", user[0].password);

                bcrypt.compare(passwd, user[0].password, function(err, res) {
                    if(res){
                        console.log("Signin OK!");
                        locals.user = user[0];
                    }else{
                        console.log("Wrong Email or Password!");
                        req.flash("error", "wrong email or password!");
                    }
                    next();
                });

            }, function (err) { //something happened
                //catch the error, it can be thrown by any promise in the chain
                console.log(err);
                next();
            });

        //next();
    });

    // Render the view
    view.render('index');
};

一切顺利! user 变量被正确读取。

怎么可能?在这两段代码中 next() 函数的位置不等价???

它们绝对不等同。你有

        aPromise
            .then(function (user) { //first promise fulfilled
                bcrypt.compare(passwd, user[0].password, function(err, res) {/*...*/});

        next();

        aPromise
            .then(function (user) { //first promise fulfilled
                bcrypt.compare(passwd, user[0].password, function(err, res) {
                    /*...*/
                    next();
                });

在您的第一个中,next() 在兑现承诺、比较密码、调用回调和 locals.user 赋值之前执行。在你的第二个中,它是在所有这些都发生之后执行的。