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:
{
[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.