【问题标题】:Knex and node.js how do i dive into the return objectKnex 和 node.js 我如何潜入返回对象
【发布时间】:2017-08-23 19:39:45
【问题描述】:

我有一个 node.js API,我使用 MySQL 和 Knex,我如何在返回中定位特定值。

db.raw('SELECT * FROM _sessions WHERE sessionkey = ?', [apiKey]).then(function(resp) {

    if (resp[0].length) {
      exports.securitycheck=true;
      console.log(resp[0].userid);
    } else {
      exports.securitycheck=false;
    }

});

但是resp[0].userid 返回未定义

这里是相应数据的日志:

[ RowDataPacket {
sessionid: 1,
sessionkey: '123abc',
userid: 1,
accountid: 1,
sessionstart: 2017-08-24T01:01:09.000Z,
sessionlast: 2017-08-24T01:01:09.000Z,
alloweddurationseconds: 1800 } ]

这里的最终目标是,如果查询返回数据,则获取 userid 和 accountid 并将它们分配给变量

【问题讨论】:

  • resp[0].length 定义了吗?
  • 你的意思可能是resp.length
  • 是的,resp[0].length 已定义
  • @Malice 即使我删除 [0] 长度仍然定义,并且日志仍然输出未定义
  • 你从哪里得到resp数据的日志?

标签: mysql node.js express knex.js


【解决方案1】:

通过使用更正

resp[0][0].userid

【讨论】:

    猜你喜欢
    • 2017-09-13
    • 2012-02-02
    • 2017-07-09
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    相关资源
    最近更新 更多