How to retrieve a list of values from a list of objects?

Viewed 159

So I have a list of objects like this formed by Sequelize query:

  [
  Likes {
    dataValues: { PlaceId: 'e61d2360-c846-46b5-aa25-6534d38276dd' },
    _previousDataValues: { PlaceId: 'e61d2360-c846-46b5-aa25-6534d38276dd' },
    _changed: Set(0) {},
    _options: {
      isNewRecord: false,
      _schema: null,
      _schemaDelimiter: '',
      raw: true,
      attributes: [Array]
    },
    isNewRecord: false
  },
  Likes {
    dataValues: { PlaceId: '417f55a6-1d64-491f-9c00-8f3094f3f53a' },
    _previousDataValues: { PlaceId: '417f55a6-1d64-491f-9c00-8f3094f3f53a' },
    _changed: Set(0) {},
    _options: {
      isNewRecord: false,
      _schema: null,
      _schemaDelimiter: '',
      raw: true,
      attributes: [Array]
    },
    isNewRecord: false
  }
]

I am getting various exceptions when querying because I get either a list like that: [{PlaceId: 'e61d2360-c846-46b5-aa25-6534d38276dd'}, {PlaceId: 'e678yt6-c846-46b5-aa25-6534d67326au'}] or an empty list: [].

I tried out Object.values as well as valueOf, map and other built in methods - even a manual for cycle and I do not really get why don't they work. What methods of achieving that can you suggest?

Solution:

return  models.Likes.findAll({
            where: {
                UserId: req.user.id
            },
            attributes: ['PlaceId']
        }).then(likes => {
            var values;
            try {
                values = likes.map(entity => entity.get('PlaceId'))
            }
            catch (e){
                console.log(e);
            }
            models.Place.findAll({
                where: {
                    id: {
                        [Sequelize.Op.notIn]: values
                    }
                },
                limit: 24
            })

This is the final working solution written thanks to one of the suggestions. Mapping with (entity => entity.get) did the trick. Best regards to everybody who commented and helped.

If anybody needs, the thing does a sequelize query from one table to then query another table with a "NOT IN". In SQL query 2ould look like that:

"SELECT "id", "coordinates", "place_name", "description", "category", "createdAt", "updatedAt" FROM "Places" AS "Place" WHERE "Place"."id" NOT IN ('e61d2360-c846-46b5-aa25-6534d38276dd', '417f55a6-1d64-491f-9c00-8f3094f3f53a') LIMIT 24;"
1 Answers

I would say, it should be sufficiant for you

const FirstTable = sequelize.model(`FirstTable`)
const list = await FirstTable.findAll({
    attributes: [`PlaceId`]
})
    .then(data => {
        return data.map(entity => entity.get('PlaceId'))
    })
console.log(list)

The output will be:

[
  'e61d2360-c846-46b5-aa25-6534d38276dd',
  'e678yt6-c846-46b5-aa25-6534d67326au'
]
Related