如何使Pug页面从我的.js文件中读取变量?
我跟随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);
bcryptpare(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
变量,应该如何。
我错过了什么吗?
如何使哈巴狗页面读取变量?
回答如下:编辑:好的,现在我看到了问题。
如果您使用的是app.all,则所有回调都应具有(req,res,next),并应在其定义中调用next()。