andehlu andehlu - 1 month ago 25
JSON Question

Parsing JSON in Jade?

I have ben scratching my head. I am trying to parse JSON in Jade. I have tried about 20 solutions I found on Stack... does anyone see what I am doing wrong here?

route (data from postgres):

//show books
pg.connect(dbconnect, function(err, client, done) {

client.query('SELECT * FROM books', function(err, result) {
res.render('books', { title: 'My Books', booklist: JSON.stringify(result.rows) });
});

});


Jade:

block content
each key in booklist
p= bookname


Output:

[{"id":1,"bookname":"Book 1"},{"id":2,"bookname":"Book 2"}]


Any help appreciated!

Answer

The problem is likely that you cast your object to a string with JSON.stringify(). That function will return a string, which you then are trying to iterate over.

Try

pg.connect(dbconnect, function(err, client, done) {
    client.query('SELECT * FROM books', function(err, result) {
        res.render('books', { title: 'My Books', booklist: result.rows });
    });
});

and

block content
   each book in booklist
       p= book.bookname
Comments