E
E
Evansive2014-04-14 21:20:13
MySQL
Evansive, 2014-04-14 21:20:13

How to pass a variable to the Jade template engine from the server side (Express.js, Node.js)?

Hello.
Server side:

io.sockets.on('connection', function(socket) {
        room.getTitle(req.param('id'), function(err, result) {
            if(result.length > 0) {
                var title = result[0].title;
                socket.join(title);
                res.render('room', { title: 'Title ' + title });
            }   
        });
    });

Here I call a method that makes a query to the database, thereby getting 'title', and joining the room on sockets with this 'title'. After that, I need to change the title of the page (title), and show the template.
Questions:
1. How to pass the "title" variable to the Jade template engine after getting the result from the database? At the moment - it doesn't work, it goes into infinite page loading (if you put rendering at the very top, then everything is fine)
2. How can I do better? I think this structure is wrong.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Pushkarev, 2014-04-15
@Evansive

Stop. Every time you load a page, you add a callback to connection socket.io, are you serious? :) The page will be rendered after res.render is called, res.render will be called after socket.io is connected, and why connect if the client connects, if I understand correctly, on a page that you haven't rendered yet? :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question