简体   繁体   中英

print SQL output in jade file

I'm trying to output an sql query in Jade file. I got it so far that I can print plain text to jade but when I try to put an output from SQL it doesn't work can someone help me.

The code in app.js (NodeJS) :

con.query('SELECT * FROM user ', function(err, rows)
{
    console.log('Connection result error '+err);
    console.log('num of records is '+rows.length);
    //console.log(rows);

    res.render('index',
        { title : 'Home' }
    )
    res.render('index',
        { row : res.send(rows) }
    )
    console.log('num of records is '+rows.length);
});

The code in jade (What I have at the moment):

  - each row in rows
        p=row.first_names

You only need to call render once like this:

res.render('index', {
  title: 'Home',
  rows: rows
});

The single call contains the object with all of the values you want passed to the Jade template. Also note the change of the key row to rows since that is what you called it in your template, as well as not calling res.send inside of res.render

You need to edit .js file (app.js in this case). And also the jade file code seems not correct

res.render('index', {title:'Home', row:rows});

Then the jade code should be changed as following. Be sure to indent properly. 'first_names' is case-sensitive

ul
each val in row
 li=val.first_names

Reference: http://jade-lang.com/reference/iteration

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