1

I'm using mongoosastic and it's working fine but the problem that I'm facing is that, how do I take the object from .post method and pass it to .get method?

For example:

router.post('/search', function(req, res, next) {
   Product.search({ something: 'Something'}, function(err, result) {
      if (err) return next(err);
      res.redirect('/search') // <--- How do i pass the result object?
   });
});

router.get('/search', function(req, res, next) {
    res.render('search');
});

I tried another approached where I use req.flash, but I don't really like that approach.

How did you guys solve this problem? it's a really basic search, where user search then it will redirect that user to another page where it either will show the found result or not found.

Aᴍɪʀ
  • 7,623
  • 3
  • 38
  • 52
Jack Moscovi
  • 2,195
  • 7
  • 30
  • 49
  • 2
    why don't you have a function that processes it? (meaning both the get function and the post function would call a common function) why do you need to pass it to the get function? Is this backend code or front end code, it looks like express? – SoluableNonagon Dec 23 '15 at 18:09
  • req.search = result; ? Then in your get('/search'), you can access req.search. It's the basic middleware approach. Think of middleware like a sequence of filter that processes your request, adding, removing, modifying data on the way. That's what you want to do ? Add the search result to the request ? – Ludovic C Dec 23 '15 at 18:10
  • 1
    why do you want to do that? you can serve the request in the `post`. – Aᴍɪʀ Dec 23 '15 at 18:11
  • @SoluableNonagon it is backend , using express.js – Jack Moscovi Dec 23 '15 at 18:15
  • @Ludo, So I need to create a custom middleware? – Jack Moscovi Dec 23 '15 at 18:16
  • No, as @Aᴍɪʀ said you can serve the response in the POST. Instead of res.redirect('/search'), do res.render('search', {search : result}). – Ludovic C Dec 23 '15 at 18:17
  • @Ludo, but i if Im not mistaken I need a .get method to serve the page? – Jack Moscovi Dec 23 '15 at 18:19
  • @Amir but i if Im not mistaken I need a .get method to serve the page? – Jack Moscovi Dec 23 '15 at 18:20
  • Only if it's a GET request, you need a router.get . If it's POST, you need a router.post. Please, read on different request types. GET POST PUT MERGE ... – Ludovic C Dec 23 '15 at 18:21
  • http://expressjs.com/en/guide/routing.html – Ludovic C Dec 23 '15 at 18:22
  • @JackMoscovi No you don't. It is perfectly acceptable to serve the request in `.post`. – Aᴍɪʀ Dec 23 '15 at 18:22
  • One of you guys should write the answer so I could accept it. It does work, but I just don't understand why .post could render a page ..? – Jack Moscovi Dec 23 '15 at 18:23
  • Post is how u accept the request, it makes no different to how u respond to it. Redirecting makes no sense here – Yerken Dec 23 '15 at 18:41
  • @JackMoscovi I just posted an answer with some more information for you to know. I'd be glad if I can help more. – Aᴍɪʀ Dec 23 '15 at 18:58

1 Answers1

4

You don't need to redirect the user to another route with GET to send the response.

You can serve the request in .post and it is perfectly acceptable.

POST and GET are two forms of HTTP Request. No matter what type of request comes to a web server, the response could be anything. It could be a redirect, or an actual web-page, or other types of things such as errors.

I don't think you need this, but just to be complete, for search pages it could be a different scenario. GET requests could be bookmarked in the browser, because all it takes to re-render the page is the URL. But POST requests could not be, because it needs post parameters as well which is in the request's body. If you want to let the users bookmark the page with the result or have a permanent link to the same page with the result, you could serve the request in GET requests (as well). Adding an extra parameter like ?q=search-term to the URL for example ...

This is a sort of sending parameters via GET request. A /search route will also catch a /search?q=search-term URL. You can have access to that using req.query.q and its value would be "search-term" (look at this question for more info). So you can either modify your form to send a GET request instead of POST (<form action="get">...) or you can redirect the user back to the search page using GET and pass the parameter along the URL. And finally serve all the search result in a GET request.

But again, this is more advanced stuff, for what you need to do, generally, it is all good to serve a request whether it is POST or GET or anything else.

Community
  • 1
  • 1
Aᴍɪʀ
  • 7,623
  • 3
  • 38
  • 52
  • 1
    Hey Amir, thank you for explaining in detail, but is it okay if you show some example because currently Im quite confused. I created a github gist https://gist.github.com/anonymous/69802ee3d24ff19f3ee0 – Jack Moscovi Dec 24 '15 at 03:14
  • @JackMoscovi the code really depends on your whole project, so do not expect this code work, it is just to give you an idea. [Here](https://gist.github.com/amir-s/70fe3de0092a560468b4). And if that answers your question, consider up-voting and marking it as the answer for future visitors :) – Aᴍɪʀ Dec 24 '15 at 03:21