Ben Davidow
Ben Davidow

Reputation: 1215

Rendering view via AJAX

I'm trying to do an infinite scroll i.e., when a user gets to the bottom of a page more content loads. I make an ajax call and then on the backend I'm trying to render the new view and return that view so then on the frontend I can append it to the existing DOM. However when I console.log(rendered) in the backend I'm getting undefined, and I'm also getting the error "Callback was already called." Specifically I'm using Express on the backend with the frontend views built in dust which is a similar to Jade etc.

router.get('/get_more_content', function(req, res) {
    ... (db parsing logic - this part works fine) ...
    rendered = res.render('galleries');
    return res.status(200).send({ rendered: rendered });
})

Upvotes: 0

Views: 220

Answers (1)

kkalamarski
kkalamarski

Reputation: 134

You are getting this error because res.render ends the connection unless you specify callback argument.

You can access html using this snippet:

res.render('galleries', function(err, html) {
  res.status(200).send({ rendered: html });
});

For more information please refer to http://expressjs.com/en/api.html#res.render

Upvotes: 2

Related Questions