skip to Main Content

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


  1. The value of findOptions.where is an object which has one property. The key of that property is the result of evaluating the expression Op.or.

    When you console.log the whole object you can see that it is a Symbol.

    findOptions.where[0]
    

    0 is not the same as Op.or, so you’re trying to access a property that doesn’t exist.

    JSON.stringify(findOptions.where)…
    

    See the documentation which says:

    All Symbol-keyed properties will be completely ignored, even when using the replacer parameter.


    If you want to access the property then you have to use its key:

     findOptions.where[Op.or]
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search