Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Using SQL ORMs with Node.js Performing CRUD Operations Attributes, Operators and Ordering

JASON LEE
JASON LEE
15,783 Points

Why the bracket syntax in Op?

I don't understand the bracket syntax [Op.gte] below. Why not just Op.gte ? I know it doesn't work without the brackets, but not understanding why.

(async () => {
  await db.sequelize.sync({ force: true });

  try {

    // ... All model instances

    const movies = await Movie.findAll({
      attributes: ['id', 'title'],
      where: {
        releaseDate: {
          [Op.gte]: '2004-01-01' // greater than or equal to the date
        }
      },
    });
    console.log( movies.map(movie => movie.toJSON()) );

  } catch(error) {
    ...
  }
})();```

1 Answer

Shane Oliver
Shane Oliver
19,965 Points

A key in a JS object can only be a string. You can use computed property name syntax (square brackets) which allows you to put a JS expression that will be computed. In this case the value at object key Op.gte. You can use the square bracket notation to use template literals as well.