I am new to Node and need some advice. I am trying to build a relational SQL query. For example I have a table named BOOKS and another named PAGES. PAGES has a foreign key pointing back to a row on BOOKS. I am trying to return a BOOK row and all the PAGES that are relational, then render to a view.
I have tried nested sql calls. Quick example below...
client.query('SELECT * FROM books WHERE id=($1) ',[bookid], function(err, result1) {
client.query('SELECT * FROM pages WHERE bookid=($1) ',[bookid], function(err, result2) {
res.render('book',{ book: result1.rows, pages: result2.rows});
done();
});
});
I have also tried chaining like below:
client.query('SELECT * FROM books WHERE id=($1); SELECT * FROM pages WHERE bookid=($2) ',[bookid, bookid], function(err, result) {
res.render('book',{ book: JSON.stringify(result.rows)});
done();
});
And both explode at runtime. Have I missed something pretty easy?