【问题标题】:Selecting multiple columns and send to jade选择多列并发送到玉
【发布时间】:2017-01-02 18:53:45
【问题描述】:

我正在使用 Node.js 构建一个 Web 应用程序,并且我正在使用以下函数从 Mysql 数据库中获取数据:

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);
    });
}

当用户登录时,我调用该函数(var x 是用户 ID):

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

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

回调返回给我这个:

[ 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 } ]

我的问题是,我怎样才能分离和检索所有列数据,并发送到我的玉布局?

我想做这样的事情:

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

谢谢。

【问题讨论】:

  • 那么你尝试了吗?什么不适合你?你检查过 Pug 中循环的文档吗? pugjs.org/language/iteration.html
  • 这个链接好像是我要找的,我会尝试实现这些代码。

标签: javascript node.js express pug


【解决方案1】:

在您的 pug 模板中,您需要在 tbody 正下方创建一个带有 each 语句的表格:

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

【讨论】:

    猜你喜欢
    • 2012-11-12
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    相关资源
    最近更新 更多