A
A
Alexey2018-04-11 11:44:45
Node.js
Alexey, 2018-04-11 11:44:45

How to pass data between functions in a route?

There is a route in which we define the user's authorization as the first function ( usersController.loadUser ).

app.get('/', usersController.loadUser, function(req, res) {
    res.render('index.ejs', { title: 'Главная страница '});
});
exports.loadUser = function(req, res, next) {
    users.loadUser(req.session.id, function(err, result) { //Отправляем запрос в БД
        if (err) {
            return res.send(err);
        } else if (result) { //Если вернулись данные из БД проверяем их и разрешаем доступ к маршруту
            next();
        } else {
            //Либо просим пройти авторизацию
            res.redirect('/signin');
        }
    });
}

How to pass data from the authorization function to the next function?
As an answer, you can give the correct search query, something I'm sensitive to hangs.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artyom Novolodsky, 2018-04-11
@skazi_premiere

well, you can add a field to req, it goes through the whole chain

D
Danil, 2018-04-11
@Veneomin

Write them to the session and read them in another function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question