gpt4 book ai didi

node.js - Express.js 模块返回未定义的值

转载 作者:太空宇宙 更新时间:2023-11-03 22:08:37 25 4
gpt4 key购买 nike

Middleware.js

exports.initLocals = function (req, res, next) {
res.locals.navLinks = [
{ label: 'Home', key: 'home', href: '/' },
{ label: 'Blog', key: 'blog', href: '/blog' },
{ label: 'Contact', key: 'contact', href: '/contact' },
{ label: 'Gallery', key: 'gallery', href: '/gallery' },
{ label : 'Research', key : 'research' , href : '/research'},
{ label : 'Publications', key : 'publications', href : '/publications'},
{ label : 'Teaching Initiatives', key : 'teaching', href : '/teaching-initiatives'}
];
res.locals.user = req.user;
userInfo = req.user;
next();
};

app.js

var middleware = require('./routes/middleware');
console.log(middleware.initLocals.userInfo);

app.js 控制台输出未定义,因为我在发出请求之前访问变量。有没有其他方法可以通过预先请求来获取 userInfo。我觉得一定有办法!

最佳答案

尝试仅打印 middleware.initLocals,或者问题可能取决于您所需的路径。希望对您有帮助:)

关于node.js - Express.js 模块返回未定义的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48974252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com