简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM