2

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 console is outputting undefined because I am accessing the variable before the request is made. Is there any other way to get the userInfo by pre-requesting. I feel that there must be way!

1 Answer 1

1

Try to print middleware.initLocals only, or maybe the problem rely on the path which you required from it . hope that help you :)

Sign up to request clarification or add additional context in comments.

5 Comments

middleware.initLocals is a function so it prints function.So, it wouldn't work! And path is perfect
It did give function @Mahmoud
The problem with mine is I have req object. It is working fine when I console it in initLocals. But when I access it from outside it is not working
ok man try this , var { initLocals } = require('./routes/middleware'); that is last one, hope it works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.