Express middleware passing variable, error 'TypeError: Cannot destructure property' in middleware function but works via route definiton

本小妞迷上赌 提交于 2020-01-25 07:58:11

问题


I am using pug templates in express and have a layout which has my top navigation toolbar etc. Inside this layout (viewable on all routes) I want to include a count of some items in the toolbar (without having to include the request for each pages/route definition ideally). So I thought I should use middleware.

I am able to use the response userProfile anywhere on the page in the /user view eg but when I try to add the const {_raw, _json, ...userProfile} = req.user; to the middleware I get the error TypeError: Cannot destructure property_rawof 'undefined' or 'null'.

Is this to do with parsing? Please help me understand. I am trying to reduce overhead therefor per route definitions would be fine but would like to understand why this call won't work, it seems like it would be very useful to display info you want on all routes.

This works but only via views (can use const in the layout but would need to redefine for each route which seems laborious),

    router.get('/user', secured(), (req, res, next) => {
        const {_raw, _json, ...userProfile} = req.user; // this works on routes
        res.render('user', {
            viewTitle: 'Profile',
            userProfile: JSON.stringify(userProfile, null, 2),
        });
    });

But this attempt (to use it on all pages via middleware) doesn't,

    module.exports = function () {
        return function (req, res, next) {
            const { _raw, _json, ...userProfile } = req.user;
            res.locals = {
                userProfile: JSON.stringify(userProfile, null, 2), // gives error in middleware but works in routes
                // userProfile: req.user, // tried this too //                    
                user: req.user,
                token: '1234',
                isAuthenticated: req.isAuthenticated(),
            };
            next();
        };
    };

app.js

app.use(express.json());// sets content-type to json
app.use(userInViews());
app.use('/', authRouter);
app.use('/', indexRouter);
app.use('/', usersRouter);
app.use('/', postRoutes);
app.use('/calendar', calendarRouter);
app.use('/bookings', usersRouter);
app.use('/add', usersRouter);
app.use('/edit', usersRouter);
app.use('/list', usersRouter);
app.use('/delete', usersRouter);
app.use('/calendar', usersRouter);
app.use('/dashboard', usersRouter);
app.use('/search', usersRouter);
app.use('/user', usersRouter);

Why can I get other variables and use them in the middleware for all routes but when calling const { _raw, _json, ...userProfile } = JSON.stringify(userProfile, null, 2), in the middleware I get the above error. What am I doing wrong please?

来源:https://stackoverflow.com/questions/59407665/express-middleware-passing-variable-error-typeerror-cannot-destructure-proper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!