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] }
]
}
2
Answers
The value of
findOptions.where
is an object which has one property. The key of that property is the result of evaluating the expressionOp.or
.When you
console.log
the whole object you can see that it is a Symbol.0
is not the same asOp.or
, so you’re trying to access a property that doesn’t exist.See the documentation which says:
If you want to access the property then you have to use its key:
As the log shows,
Op.or
is a symbol. Those are typically ignored as properties, at least byObject.keys
,Object.getOwnPropertyNames
orfor … in
loops – you’d need to checkObject.getOwnPropertySymbols
to test whether there are any symbol-keyed properties in the object.You access the property using
findOptions.where[Op.or]
, notfindOptions.where[0]
– it’s not an array.