简体   繁体   中英

Selecting multiple columns and send to jade

I'm building an web application using Node.js, and I'm getting data from my Mysql database using the following function:

function getRobots(robotname, ownerid, callback) {
    connection.query('SELECT * FROM robots WHERE robot_owner_id = ?', ownerid, function(err, rows, fields) {
        if (err) callback(err, null);
        else 
        callback(null, rows);
    });
}

And when the user Logs in I call the function (var x is the user id):

        getRobots("robot", x, function(err, data) {
            if (err) {

            console.log("ERROR : ", err);
            } else {
            console.log(data);
            res.render('logged_in', {
            data: data
            });
            }
        });

The callback returns me this:

[ RowDataPacket {
    robot_id: 1,
    robot_name: 'one',
    robot_status: 1,
    robot_owner_id: 35 },
  RowDataPacket {
    robot_id: 2,
    robot_name: 'ASAS',
    robot_status: 1,
    robot_owner_id: 35 } ]

And my question is, how can I separate and retrieve all the columns data, and send to my jade layout?

I would like to do something like this:

<ul>
<li>Robot 1 | Id 1</li>
<li>Robot 2 | Id 2</li>
...

Thank you.

In your pug template you need to create a table with an each statement right below the tbody:

table
  thead
    tr
      th ID
      th Name
      th Status
      th OwnerID
  tbody
    each robot in data
      tr
        td= robot.id
        td= robot.name
        td= robot.status
        td= robot.owner_id

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