Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why can't I access the properties of a sequelize object with keys in the form '[Op.or]'?

I am building a sequelize query. While doing some debugging I was surprised to find that I couldn’t access the properties of my options.where object, but that console.log still returned the expected object structure.
My question is (1) why does this occur and (2) how do I get around the problem to access the properties?

My code looks like this:

findOptions.where = {
      [Op.or]: [
        {
          id: {
            [Op.iLike]: `%${search}%`,
          },
        },
        {
          serialNumber: {
            [Op.iLike]: `%${search}%`,
          },
        },
    ]};

This uses, so far as I can tell, standard sequelize syntax. However, _.isEmpty(findOptions.where) evaluates to true, findOptions.where[0] evaluates to unknown, JSON.stringify(findOptions.where) returns {} and Object.keys(findOptions.where) returns an empty array. Despite this, console.log returns the expected result:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
  [Symbol(or)]: [
    { id: [Object] },
    { serialNumber: [Object] }
  ]
}

>Solution :

As the log shows, Op.or is a symbol. Those are typically ignored as properties, at least by Object.keys, Object.getOwnPropertyNames or for … in loops – you’d need to check Object.getOwnPropertySymbols to test whether there are any symbol-keyed properties in the object.

You access the property using findOptions.where[Op.or], not findOptions.where[0] – it’s not an array.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading