4

I have the following query in 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;

I'm importing it in a file then work the data through a library called Knex.js

When I run it and console log the query, I get the following structure:

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

Which is almost what I need, however, I just need the memberId data, is there any way how I could map through it to only get back the memberId properties?

I have tried this but it doesn't seem to work:

  const users = userState.map(users => ({
      memberId: memberId;
  }))
Dan D.
  • 73,243
  • 15
  • 104
  • 123

3 Answers3

1

You'll need to convert that structure to a valid JSON first. And then you may process on userState.rows if you want.

  • Here is a reference for you to convert knex.js response to JSON.

  • Here is a working example using a valid 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);
Tushar Walzade
  • 3,737
  • 4
  • 33
  • 56
0

Try this

const userState = {
    rows: [
        { memberId: 1 },
        { memberId: 2 }
    ]
}

const users = userState.rows.map(users =>
    users.memberId
)

console.log(users)

// [ 1, 2 ]
Joss Classey
  • 1,054
  • 1
  • 7
  • 22
0

I think this will help you: https://knexjs.org/#Interfaces-map

I think error at

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

That should be:

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

You can try my solution, it maybe work, because I not use Knex.js for SQL with Node.JS, I usually Sequelize

Ha. Huynh
  • 1,772
  • 11
  • 27