0

I am trying to send data from MongoDB through a redirect using Express.

I am fairly new to working with the server and connecting it to a front-end, so please be easy on me if I have any huge misunderstandings.

Here is an example of my code and what I am trying to do:

app.get('/dashboard', requireLogin, function(req, res) {
  res.render('dashboard.jade');
});
app.get('/api/posts',function(req, res) {
  return Post.find(function(err, posts) {
    if (!err) {
      console.log('Retrieving all posts.');
      res.send(posts);
      res.redirect('/dashboard');
    } else {
      return console.log(err);
    }
  });
}); 

When I render dashboard.jade I have data that is being used in the template to display posts. What I'd like to do is make any GET calls to /api/posts retrieve the posts data from Mongo and send it to the route /dashboard with the post data it retrieved.

Zach
  • 4,555
  • 9
  • 31
  • 52
  • 1
    If you're using a redirect, you're going to want to store the data in a flash session. [Flash](https://github.com/expressjs/flash) is a good start. – Ben Fortune Mar 26 '15 at 15:48
  • possible duplicate of [Node & Express - How do I send a page AND some JS data to the browser in a single request?](http://stackoverflow.com/questions/21172889/node-express-how-do-i-send-a-page-and-some-js-data-to-the-browser-in-a-singl) – kshirish Jun 24 '15 at 09:38

0 Answers0