1

I have a query that returns results from my database, but I don't see how I can have it give me results from multiple queries.

router.get("/", function(req, res) {
  pg.query("SELECT * from tic", (err, done) => {
    if (err) {
      console.log(err);
    }
    res.render("index", { tic: done.rows });
  });
});

I was trying along the lines of this, but cant get it to work since he render statement is inside the query and I can get the render to see the results when I move it out of there

router.get("/", function(req, res) {
  pg.query("SELECT * from tic", (err, tic) => {
    if (err) {
      console.log(err);
    }
  pg.query("SELECT * from tac", (err, tac) => {
    if (err) {
      console.log(err);
    }
  });
  res.render("index", { tic: tic.rows }, { tac: tac.rows});
});
1
  • wrap both the queries in promises and combine their results once both are done Commented Aug 4, 2018 at 17:15

1 Answer 1

1

You just have to render after the second query executes. To do that you have to render in the callback of the second query.

router.get("/", function (req, res) {
    pg.query("SELECT * from tic", (err, tic) => {
        if (err) {
            console.log(err);
        }
        pg.query("SELECT * from tac", (err, tac) => {
            if (err) {
                console.log(err);
            }
            res.render("index", { tic: tic.rows }, { tac: tac.rows });
        });
    });
});
Sign up to request clarification or add additional context in comments.

Comments

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.