【问题标题】:Iterate through this structure?遍历这个结构?
【发布时间】:2019-05-12 08:51:41
【问题描述】:

我在 PostgreSQL 中有以下查询:

select bookings."memberId" from bookings
join "memberConnections" mbc on bookings."memberId" = mbc."memberId"
join shifts shf on shf.id = bookings."shiftId"
where bookings.state = 'WAITING_LIST' and (mbc.state = 'LOCKED' or mbc.state = 'REMOVED')
and shf."startTime" > CURRENT_TIMESTAMP;

我将它导入到一个文件中,然后通过一个名为 Knex.js

当我运行它并在控制台记录查询时,我得到以下结构:

USER_STATE: Result {
  command: 'SELECT',
  rowCount: 32,
  oid: NaN,
  rows: 
   [ anonymous { memberId: 1800 },
     anonymous { memberId: 15476 },
     anonymous { memberId: 15476 },
     anonymous { memberId: 15476 },
     anonymous { memberId: 12553 },
     anonymous { memberId: 12553 },
     anonymous { memberId: 19668 },
     anonymous { memberId: 19668 },
     anonymous { memberId: 21004 },
     anonymous { memberId: 21004 },
     anonymous { memberId: 21004 },
     anonymous { memberId: 21004 },
     anonymous { memberId: 16105 },
     anonymous { memberId: 14927 },
     anonymous { memberId: 15476 },
     anonymous { memberId: 12553 },
     anonymous { memberId: 12553 },
     anonymous { memberId: 12553 },
     anonymous { memberId: 12553 },
     anonymous { memberId: 17923 },
     anonymous { memberId: 17273 },
     anonymous { memberId: 12553 },
     anonymous { memberId: 12553 },
     anonymous { memberId: 19429 },
     anonymous { memberId: 17312 },
     anonymous { memberId: 17273 },
     anonymous { memberId: 15476 },
     anonymous { memberId: 15476 },
     anonymous { memberId: 15476 },
     anonymous { memberId: 15476 },
     anonymous { memberId: 15476 },
     anonymous { memberId: 19634 } ],
  fields: 
   [ Field {
       name: 'memberId',
       tableID: 22531,
       columnID: 3,
       dataTypeID: 23,
       dataTypeSize: 4,
       dataTypeModifier: -1,
       format: 'text' } ],
  _parsers: [ [Function: parseInteger] ],
  RowCtor: [Function: anonymous],
  rowAsArray: false,
  _getTypeParser: [Function: bound ] }

这几乎是我需要的,但是,我只需要 memberId 数据,有什么方法可以通过它映射以仅取回 memberId 属性?

我试过了,但它似乎不起作用:

  const users = userState.map(users => ({
      memberId: memberId;
  }))

【问题讨论】:

  • 你不应该去 userState.rows.map 吗?
  • 我试过了,它说它无法读取未定义的属性 memberId,这真的很奇怪,通常我对这样的东西没有问题,但我是 postgres 的新手,也许我是缺少一些东西
  • 你能举一个这个问题的例子吗?在 jsfiddle 中可能

标签: javascript arrays node.js object knex.js


【解决方案1】:

您需要先将该结构转换为 valid JSON。然后您可以根据需要在userState.rows 上处理。

  • Here 是您将 knex.js 响应转换为 JSON 的参考。

  • 这是一个使用有效 JSON 的工作示例 -

var userState = {
    rows:
        [{ memberId: 1800 },
        { memberId: 15476 },
        { memberId: 15476 },
        { memberId: 12553 },
        { memberId: 12553 },
        { memberId: 19668 },
        { memberId: 19668 },
        { memberId: 21004 },
        { memberId: 21004 },
        { memberId: 19634 }],
    fields:
        [{
            name: 'memberId',
            tableID: 22531,
            columnID: 3,
            dataTypeID: 23,
            dataTypeSize: 4,
            dataTypeModifier: -1,
            format: 'text'
        }]
};

var data = userState.rows.map((users, index) =>
    users.memberId
);

console.log(data);

【讨论】:

    【解决方案2】:

    试试这个

    const userState = {
        rows: [
            { memberId: 1 },
            { memberId: 2 }
        ]
    }
    
    const users = userState.rows.map(users =>
        users.memberId
    )
    
    console.log(users)
    
    // [ 1, 2 ]
    

    【讨论】:

      【解决方案3】:

      我想这会对你有所帮助:https://knexjs.org/#Interfaces-map

      我认为错误在

      .map(users => ({
            memberId: memberId;
        }))
      

      应该是:

      .map(function(row) {
        return row.memberId;
      })
      

      您可以尝试我的解决方案,它可能会起作用,因为我不会将 Knex.js 用于带有 Node.JS 的 SQL,我通常是 Sequelize

      【讨论】:

        猜你喜欢
        • 2012-08-25
        • 1970-01-01
        • 2014-11-14
        • 2011-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多