Sequelize: overlapping – Checking if any value in array matches any value in the passed array

Issue

Using sequelize to check if the the db input property, which is array, has a given item.
Have a Postgres database with data Events.
Want to get one Event that will have any of these weekDays.
Type of weekDays is ARRAY(integer).

Events.findOne({
  where: {
    weekDays: {
      $contains: [2, 3],
    },
  },
});

Tried to do with $contains, $any, or $like $any but all the time got the same error message.

TypeError: values.map is not a function

Sincerely thanks

Solution

You are looking for overlapping values. Any value in you weekDays array that matches any values in your passed in array.

As such, you can use the PG specific Sequelize.Op.overlap operator:

See documentation here

Can then be used like

Events.findOne({
  where: {
    weekDays: {
      [Sequelize.Op.overlap]: [2, 3],
    },
  },
});

Answered By – Michael McCabe

Answer Checked By – Candace Johnson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.