Lazarus Rising
Lazarus Rising

Reputation: 2675

Jade page being rendered multiple times

I have a jade page that takes a parameter (username) from the url and renders the info about this user. However, the page is being rendered several times. When I print the url, the first time is seems as if it comes complete (with a username) and the other times it does not. I am using node js and angular to serve the data. Here is my code:

NODE JS: (myuser is a static value of an existing user that I am testing)

router.get('/personel', function (req, res) {
    var user_name = req.query['username']; 
    var fullname= 0;
    var fields = ['fullname'];

    if(req.url == '/personel?username=myuser'){
        models.users.findAll({
            attributes: fields,
            limit: 1,
            where: {username: user_name}
        }).then(function (result) {

            res.render('personel',
            {       
                title : 'Personel', 
                username: user_name,
                fullname: result[0].fullname,
            })
        }); 
        console.log(req.url);
    }
});

The controller:

Todos.get().success(function(data) {
    if ($scope.todosi != undefined) {
        $scope.todos = data;
        $scope.loading = false;
    }
});

JADE code:

form
        input(type='text', ng-model='todosi.username', value=username)
        table.table.table-hover
          tr
            td full name
            td
              input(type='text', ng-model='todosi.fullname', placeholder=fullname)
        button.btn.btn-primary.btn-lg(type='submit', ng-click='createTodo()') Update

So if I load the page as http://localhost:31/apiv2/customer/personel?username=myuser in the browser, the username is empty but fullname has the value of the actual full name.

In the logs, with req.url I get /personel?username=myuser initially and then only /personel

However in Mozilla Developer tools -> Network, the referer is always http://localhost:31/apiv2/customer/personel?username=myuser

I tried to force the page to render only when the url included the username as a parameter but it does not change anything

PS: Despite what it might look here, jade is correctly intended and not giving any errors.

Upvotes: 3

Views: 66

Answers (1)

gianlucatursi
gianlucatursi

Reputation: 680

First, if you have a query param the better way is:

router.get('/personel/:username', function (req, res) {});

Second I think that your page is loaded several times because you have different (but same) routes.

If you use this solution:

router.get('/personel/:username', function (req, res) {});

check the order of your routes. This order does not working:

 /personel/:username
 /personel/sayhello

because sayhello it is catch as :username

Upvotes: 1

Related Questions