Rahul Sharma
Rahul Sharma

Reputation: 190

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

Upvotes: 0

Views: 730

Answers (2)

kavinda
kavinda

Reputation: 758

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

Upvotes: 0

mynameiscoffey
mynameiscoffey

Reputation: 15992

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

Upvotes: 1

Related Questions