# Smart field search v8

**URL:** <https://community.forestadmin.com/t/smart-field-search-v8/3533>\
**Category:** Help me!\
**Created:** [October 13, 2021, 3:53pm UTC](https://community.forestadmin.com/t/smart-field-search-v8/3533 "2021-10-13T15:53:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tatsyana\_Slabodchyka](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/tatsyana_slabodchyka/32/1488_2.png) [@Tatsyana\_Slabodchyka](https://community.forestadmin.com/u/Tatsyana_Slabodchyka)\
**Post date:** [October 13, 2021, 3:53pm UTC](https://community.forestadmin.com/t/smart-field-search-v8/3533/1 "2021-10-13T15:53:21Z")

</div>

## Expected behavior

`query.include` parameter of a smart field `search` function contains some included tables (say, A and B). I am able to edit the array and add some more nested includes to table A. This is how it works in v7.

## Actual behavior

`query.include` is an empty array in the function. I add a new `include` (table A and it’s nested tables). Then in the logs I see the query `include` tables: A, B (as in the v7 I guess) and one more A with nested tables (that was added by me). So the table A is included twice. This is in v8.

## Failure Logs

Also if `include` is not a `[{ model: A }]` but a `{ model: A }`, error `parentIncludeList.forEach` is thrown

## Context

Please provide any relevant information about your setup.

- Package Version: 8.3.0
- Express Version: 4.17.1
- Sequelize Version: 5.22.4
- Database Dialect: mysql
- Database Version:
- Project Name:

---

<div class="post-metadata">

**Author:** ![morganperre](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/morganperre/32/1296_2.png) [@morganperre](https://community.forestadmin.com/u/morganperre)\
**Post date:** [October 14, 2021, 7:59am UTC](https://community.forestadmin.com/t/smart-field-search-v8/3533/2 "2021-10-14T07:59:15Z")

</div>

Hello @Tatsyana_Slabodchyka,

Thanks for your feedback. We recently (yesterday) found the issue. We have a ticket on our internal tools. I will add this thread to be sure your updated once a fix is released. 🙂

Kind regards,  
Morgan

---

<div class="post-metadata">

**Author:** ![morganperre](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/morganperre/32/1296_2.png) [@morganperre](https://community.forestadmin.com/u/morganperre)\
**Post date:** [October 20, 2021, 3:02pm UTC](https://community.forestadmin.com/t/smart-field-search-v8/3533/3 "2021-10-20T15:02:42Z")

</div>

Hello @Tatsyana_Slabodchyka,

I’m pleased to announce that we released a new version of [forest-express-sequelize](https://www.npmjs.com/package/forest-express-sequelize/v/8.4.3) ! 🎉

Could you update to the new version ? And let me know if it solves your issue

Kind regards,  
Morgan

---

<div class="post-metadata">

**Author:** ![Tatsyana\_Slabodchyka](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/tatsyana_slabodchyka/32/1488_2.png) [@Tatsyana\_Slabodchyka](https://community.forestadmin.com/u/Tatsyana_Slabodchyka)\
**Post date:** [October 21, 2021, 6:22am UTC](https://community.forestadmin.com/t/smart-field-search-v8/3533/4 "2021-10-21T06:22:32Z")

</div>

Hi! So I updated the version locally and checked - I got an error:

```javascript
[forest] 🌳🌳🌳 Unexpected error: Cannot read property 'target' of undefined
{
  "stack": "TypeError: Cannot read property 'target' of undefined\n at .../node_modules/forest-express-sequelize/dist/utils/sequelize-compatibility.js:161:47\n at Array.forEach (<anonymous>)\n at removeDuplicateAssociations (.../node_modules/forest-express-sequelize/dist/utils/sequelize-compatibility.js:158:15)\n at Object.exports.postProcess (.../node_modules/forest-express-sequelize/dist/utils/sequelize-compatibility.js:178:3)\n at QueryOptions.get sequelizeOptions [as sequelizeOptions] (.../node_modules/forest-express-sequelize/dist/services/query-options.js:75:44)\n at ResourcesGetter._buildQueryOptions (.../node_modules/forest-express-sequelize/dist/services/resources-getter.js:116:35)\n at async ResourcesGetter.count (.../node_modules/forest-express-sequelize/dist/services/resources-getter.js:44:30)"
}

```

---

<div class="post-metadata">

**Author:** ![morganperre](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/morganperre/32/1296_2.png) [@morganperre](https://community.forestadmin.com/u/morganperre)\
**Post date:** [October 21, 2021, 7:41am UTC](https://community.forestadmin.com/t/smart-field-search-v8/3533/5 "2021-10-21T07:41:17Z")

</div>

Hey @Tatsyana_Slabodchyka,

We are currently checking. 😓  
Can you provide your **sequelize** version ?

Also, can you provide the Smart Field that give that error ?

Thanks, and sorry for the false joy.

Regards,  
Morgan

---

<div class="post-metadata">

**Author:** ![Tatsyana\_Slabodchyka](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/tatsyana_slabodchyka/32/1488_2.png) [@Tatsyana\_Slabodchyka](https://community.forestadmin.com/u/Tatsyana_Slabodchyka)\
**Post date:** [October 21, 2021, 8:44am UTC](https://community.forestadmin.com/t/smart-field-search-v8/3533/6 "2021-10-21T08:44:30Z")

</div>

- sequelize@5.22.4
- smart field, `companyNameQuery.include[0]` used to be the `bA` table

```javascript
{
      field: 'company_name',
      type: 'String',
      reference: 'organization.organizationId',
      async get(object) {
        // ...
      },
      search(query, search) {
        const companyNameQuery = query;
        companyNameQuery.include[0] = {
          ...companyNameQuery.include[0],
          include: [{
            model: getModels().fS,
            as: 'fS',
            include: {
              model: getModels().organization,
              as: 'organization',
            },
          }],
        };
        companyNameQuery.where[Op.and][0][Op.or]
          .push(getModels().sequelize.literal(`\`bA->fS->organization\`.\`companyName\` LIKE '%${search}%'`));
        return companyNameQuery;
      },
    },

```

- Thanks for the investigation 🙂

---

<div class="post-metadata">

**Author:** ![morganperre](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/morganperre/32/1296_2.png) [@morganperre](https://community.forestadmin.com/u/morganperre)\
**Post date:** [October 21, 2021, 10:10am UTC](https://community.forestadmin.com/t/smart-field-search-v8/3533/7 "2021-10-21T10:10:23Z")

</div>

Hey @Tatsyana_Slabodchyka,

After some investigations we have spotted an error on our side, also the error sent by **forest-express-sequelize** was not the right one,.

We just released a new version. Let me know if it solves your issue this time. 🙏

_It could also raise a sequelize error if you have a wrong include. Looking at your code the include seems suspicious. Why do you use the spread `...companyNameQuery.include[0]` ?_

```javascript
// It should be more something like that
companyNameQuery.include.push({
    include: [{
      model: getModels().fS,
      as: 'fS',
      include: {
        model: getModels().organization,
        as: 'organization',
       },
     }],
});

```

Regards,  
Morgan

---

<div class="post-metadata">

**Author:** ![Tatsyana\_Slabodchyka](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/tatsyana_slabodchyka/32/1488_2.png) [@Tatsyana\_Slabodchyka](https://community.forestadmin.com/u/Tatsyana_Slabodchyka)\
**Post date:** [October 22, 2021, 2:35pm UTC](https://community.forestadmin.com/t/smart-field-search-v8/3533/8 "2021-10-22T14:35:37Z")

</div>

Hello!  
So if I run the sever without changing anything I got another error: `Unexpected error: Include unexpected. Element has to be either a Model, an Association or an object`. And the `query.include` in the search function is an empty array.  
If I include the `bA` table (not fS) - it works perfectly. Though I didn’t have a chance to check the final includes (if there are any tables included multiple times)

---

<div class="post-metadata">

**Author:** ![morganperre](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/morganperre/32/1296_2.png) [@morganperre](https://community.forestadmin.com/u/morganperre)\
**Post date:** [October 22, 2021, 3:44pm UTC](https://community.forestadmin.com/t/smart-field-search-v8/3533/9 "2021-10-22T15:44:55Z")

</div>

Hey @Tatsyana_Slabodchyka,

> [@Tatsyana\_Slabodchyka](#):
>
> If I include the `bA` table (not fS) - it works perfectly. Though I didn’t have a chance to check the final includes (if there are any tables included multiple times)

Good news. We have developed a system that optimize your query. It automatically removes duplicated includes.

We have done this since it could be hard to understand the state of the includes when you have multiple Smart Fields, all with search functions, on the same collection.

Kind regards,  
Morgan
