Error when creating Smart Collection

Hello Forest,

I’m implementing a Smart Collection using the example in the documentation but I’m facing this error Unexpected error: models.query is not a function

Here is my code :

/routes/customer-success-dashboard.js

const { RecordSerializer} = require('forest-express-sequelize');
const express = require('express');
const router = express.Router();
const models = require('../models');
const { QueryTypes } = require('sequelize');



router.get('/customer_success_dashboard', (req, res, next) => {
  const limit = parseInt(req.query.page.size) || 20;
  const offset = (parseInt(req.query.page.number) - 1) * limit;
  const queryType = QueryTypes.SELECT;
  let conditionSearch = '';

  if (req.query.search) {
    conditionSearch = `user.email LIKE '%${req.query.search.replace(/\'/g, '\'\'')}%'`;
  }

  const queryData = `
    SELECT user.email,
    FROM user
    LIMIT ${limit}
    OFFSET ${offset}
  `;

  const queryCount = `
    SELECT COUNT(*),
    FROM user
    LIMIT ${limit}
    OFFSET ${offset}
  `;

  Promise.all([
    models.query(queryData, { type: queryType }),
    models.query(queryCount, { type: queryType }),
  ])
    .then(async ([userList, userCount]) => {
      const customerStatsSerializer = new RecordSerializer({ name: 'user_stats' });
      const customerStats = await customerStatsSerializer.serialize(userList);
      const count = userCount[0].count;
      res.send({ ...customerStats, meta:{ count: count }});
    })
    .catch((err) => next(err));
});

module.exports = router;

Am I missing an import ?

Here is my setup :

  • Express Version: 4.17
  • Sequelize Version: 5.15 (Forest Express 7.0.0)
  • Database Dialect: SQL

Hello @gaelperon,

Could you try to replace:

models.query

to

models.sequelize.query

Tell me if it helps :wink:

Hello @Guillaume_Cisco

Thanks for the answer.

Same problem with models.sequelize.query

I get. : Unexpected error: Cannot read property 'query' of undefined

1 Like

Hi @gaelperon,

I’m not sure if you switched from v6 to v7, or just generated a project in v7. I’ll detail both just in case:

Switched from v6 to v7

As stated here, models.sequelize will not work anymore, and should be replaced by something you should have configured. You can debug this by logging models.connections, or directly try models.connections.default instead of models.sequelize if you used the default models/index.js file provided in the documentation link I mentionned earlier.

Directly generate a project in v7 (With a single database connected to your backend)

You should be able to make models.connections.default work out of the box. Let me know if that’s not the case.

Just to be sure, I still did a quick test on my end, which works as you can see here

For a bit of context, this changes was introduced recently to ease the configuration of multiple database setup, and models.connections will contains all the connections made to multiple database, allowing you to select (from code) on which database you want to do your query.

In the meantime I’ll forward this in order to update the documentation of smart collections.

Let me know if it helps :pray:

Thanks @jeffladiray, your solution works

To also add context, I updated to V7 using a diff between my install and a fresh generated install because the upgrade documentation you mention is incomplete (missing to mention some files).

And here it’s also a documentation problem.

1 Like

You did encounter other issues while migrating to v7?
We would be happy to have more insights to improve our documentation/migration to v7 process :raised_hands:

You can take a project in the previous version + follow the steps of migration in your guide and see what’s missing to make it work.

A few files you need to migrate are not mentioned in the doc (I don’t remember which one)