Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Issues with { 0p }

Having some issues with importing the 'Op' operators

When using the code from the lesson I get an error that Op is undefined

When I use the code from the Sequelize docs I get an error of s.replace is undefined

Here is the code I have at the beginning of app.js

const db = require('./db');
const { Movie, Person } = db.models;
//const { Op } = require('sequelize');
const { Op } = db.Sequelize;

I don't have both the third and fourth line running at the same time, I just put them both in for this example. I'm running version 5.22.4 of Sequelize. Any ideas? Thanks.

Hi Jason.

Doing some debugging and I had a similar issue where it was not recognizing Op (not yet in use). In the debugger console I set a watch on Op and it was showing up correctly, until after it ran the asynchronous queries such as const movies = await Movie.findAll({ ..... Afterwards, Op changed to "Uncaught ReferenceError ReferenceError: Op is not defined".

Now, when I use Op in the async query section, Op works fine.

Here is my code at the top

const db = require('./db'); // or require('./db/index.js')
const {Movie} = db.models; // short hand form for writing Movie = db.models.Movie
const Person = db.models.Person;
const {Op} = db.Sequelize;

Now here is without using Op

      const movieById = await Movie.findByPk(1);
      const movieByRuntime = await Movie.findOne({where: {runtime: 115}});
      const movies = await Movie.findAll({
         where: {
            // isAvailableonVHS: false,
            releaseDate: {
               // [Op.gte]: "2004-01-01"
            }
         },
         attributes: ["id", 'title']
      });
      console.log("testing")   // this is where the debugger stop was placed

Now here is with using Op

      const movieById = await Movie.findByPk(1);
      const movieByRuntime = await Movie.findOne({where: {runtime: 115}});
      const movies = await Movie.findAll({
         where: {
            // isAvailableonVHS: false,
            releaseDate: {
               [Op.gte]: "2004-01-01"
            }
         },
         attributes: ["id", 'title']
      });
      console.log("testing")  // this is where the debugger stop was placed

Just bizarre.

1 Answer

Adebayo Ojo
Adebayo Ojo
23,661 Points

Check the content of your index.js file if it follows the below:

const Sequelize = require('sequelize');

const sequelize = new Sequelize({
  dialect: 'sqlite',
  storage: 'movies.db',
  logging: false
});

const db = {
  sequelize,
  Sequelize,
  models: {},
};

db.models.Movie = require('./models/movies.js')(sequelize);
db.models.Person = require('./models/person.js')(sequelize);

module.exports = db;