我很困惑这里发生了什么。如果用户当前没有,我正在尝试为用户设置 res.locals 默认个人资料图片。这是我的代码:
// Make user object available in templates.
app.use(function(req, res, next) {
res.locals.user = req.user;
if (req.user && req.user.profile) {
console.log('Request Picture: ', req.user.profile);
res.locals.user.profile.picture = req.user.profile.picture || defaults.imgs.profile;
console.log('Request Picture After Locals: ', req.user.profile);
}
next();
});
// Console Results
Request Picture: { picture: '',
website: '',
location: '',
gender: '',
name: 'picture' }
Request Picture After Locals: { picture: '/img/profile-placeholder.png',
website: '',
location: '',
gender: '',
name: 'picture' }
我希望能够编写 JADE 而不必处理这样的事情:img(src=user.profile.picture || defaults.profile.picture)
. 所以上面的代码在所有 JADE 视图中都可以正常工作。
但是,我需要检查req.user.profile.picture
其他地方才能更改图片。
if (!req.user.profile.picture) {do stuff}
正如你在上面看到的req
已经改变了。设置res.locals
不应该改变req
对象...正确!?还是我错过了什么?
谢谢你的帮助!