Cannot read property 'Op' of undefined

Hi,

I just updated from v6 to v7, and my search on my smart fields are not working anymore.

Expected behavior

In V6, I was using this imports:

const { collection } = require('forest-express-sequelize');
const models = require('../models/');
const { Op } = models.Sequelize;

Now, I looked in the documentation and took new oens:

const { collection } = require('forest-express-sequelize');
const models = require('../models/');
const _ = require('lodash');
const Op = collection.sequelize.Op;

Normally, Op is working properly.

Actual behavior

But I have the error:
TypeError: Cannot read property 'Op' of undefined.

Context

  • Forest Express: 8.5.1
  • Express Version: 4.17
  • Sequelize Version: 7.9.1
  • Database Dialect: PostgreSQL
  • Database Version: 13

Thanks for your help

Hello @LotusBlack,

It seems that you still can use the old import. The declaration of sequelize in your model completely depends on your file located at models/index.js. As it should contain the lines below, it should work.

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

Where did you find the new declaration in the docs? I need to check if these lines only apply with newly generated projects.

Hi @GuillaumeGautreau,

Thanks a lot for your answer !

The documentation I was looking at is this page.

The new index.js file I am using is also based on your documentation too.

I am declaring Sequelize like that :
const Sequelize = require('sequelize');

I managed to resolve the issue. Since we need to have a objectMapping object declared in the index.js file because of authentification, we have to import objectMapping:
const Op = models.objectMapping.Op;

1 Like

I suppose you could also update this part of the documentation: Populate a postgreSQL database on Heroku - Documentation.

As you need the variable APPLICATION_URL to login.

Hello @LotusBlack :wave:

You right indeed, this documentation was not up-to-date with the latest version, and const Op = models.objectMapping.Op; is indeed a solution to your issue. Thanks for your feedback, I’ll push an update to our documentation :raised_hands:

From what I’ve tested this morning, the models/index.js looks valid in the documentation.

However, I do not understand what you mean by

I suppose you could also update this part of the documentation: Populate a postgreSQL database on Heroku - Documentation .

This documentation is mostly here to present the database dump re-import on postgres-heroku, which should not need APPLICATION_URL to be achieved. The v7 upgrade note mentions that a new environment variable is required with the v7 here. However, I do agree that the section is quite small, and since this variable is really important to make the deployment/upgrade work, it might be worth it to respecify this elsewhere in the documentation.

Again, thanks for spotting this issue in our documentation :pray:

2 Likes

Hello @jeffladiray

You’re welcome.
Thank you for the reply and for updating the doc!

Yes you can forget what I say about this. It is because I am using the config vars space in heroku to configure my variable, so I needed to add the APPLICATION_URL variable to make it work.