0

I am getting my search result as JSON format from the URL: "/accounts/:id/users" and I need to retrieve that JSON data to render "/contacts/find". (The latter page shows search results.)

Here is my app router. With this I could see the result of the search in /contacts/find in JSON format:

app.post('/contacts/find', function(req, res) {
  var searchStr = req.param('searchStr', null);
  if ( null == searchStr ) {
    res.send(400);
    return;
  }

  models.Account.findByString(searchStr, function onSearchDone(err,accounts) {
    if (err || accounts.length == 0) {
      res.send(404);
    } else {
      res.send(accounts);
    }
  });
});

How can I use "accounts" (in JSON format) and re-render the page with the account info? I am trying with the below code snippet, but it does not work.

app.get('/contacts/find', function(req, res) {
 res.render('searchResult.ejs', {
  accounts: req.accounts,
  email: req.accounts.email
 })
}

The app.get method is not working. The URL just shows JSON data.

1 Answer 1

1

So accounts is a JSON string? In that case, you can parse it (to convert it back to an object) and pass the result to your template:

var accounts = JSON.parse(req.accounts);
res.render('searchResult.ejs', {
  accounts : accounts,
  email    : accounts.email
 })
Sign up to request clarification or add additional context in comments.

4 Comments

Is it ok if I render the page within the "app.post" method without using "app.get" method ?
@eknbrk sure, I don't see why not. Just replace the res.send in your POST route with the res.render
thanks again @robertklep.. It worked but, it page renders <%email%> as "undefined".. Do you have any idea ?
@eknbrk well sounds like accounts.email might be undefined?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.