# Bug report with Smart Models on Node.js agent

**URL:** https://community.forestadmin.com/t/bug-report-with-smart-models-on-node-js-agent/6480
**Category:** Help me!
**Created:** [July 10, 2023, 2:36pm UTC](https://community.forestadmin.com/t/bug-report-with-smart-models-on-node-js-agent/6480 "2023-07-10T14:36:40Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![David\_Roegiers](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/david_roegiers/32/1039_2.png) [@David\_Roegiers](https://community.forestadmin.com/u/David_Roegiers)
#### Post date: [July 10, 2023, 2:36pm UTC](https://community.forestadmin.com/t/bug-report-with-smart-models-on-node-js-agent/6480/1 "2023-07-10T14:36:40Z")

</div>

## Feature(s) impacted

Model:

```javascript
...
    languages: {
      type: [
        {
          value: {
            lang: String,
            kind: String,
          },
          _id: false,
        },
      ],
      required: true,
      default: [],
    },
...

```

configuration

```javascript
...
agent.addDataSource(
  createMongooseDataSource(connection, {
    flattenMode: 'manual',
    flattenOptions: {
      offices: { asFields: ['address', 'postbox'], asModels: ['lawyers'] },
      lawyers: {
        asFields: ['billing', 'languages', 'experience', 'education', 'practiceAreas', 'publications'],
        asModels: ['languages', 'experience', 'education', 'practiceAreas', 'publications'],
      },
    },
  })
);
...

```

leads to `forestadmin-schema.json`:

```javascript
...
    {
      "actions": [],
      "fields": [
        {
          "defaultValue": null,
          "enums": null,
          "field": "_id",
          "integration": null,
          "inverseOf": null,
          "isFilterable": true,
          "isPrimaryKey": true,
          "isReadOnly": true,
          "isRequired": false,
          "isSortable": true,
          "isVirtual": false,
          "reference": null,
          "type": "String",
          "validations": []
        },
        {
          "defaultValue": null,
          "enums": null,
          "field": "parent",
          "integration": null,
          "inverseOf": "languages",
          "isFilterable": true,
          "isPrimaryKey": false,
          "isReadOnly": false,
          "isRequired": true,
          "isSortable": true,
          "isVirtual": false,
          "reference": "lawyers._id",
          "relationship": "BelongsTo",
          "type": "String",
          "validations": [{"type": "is present", "message": "Field is required"}]
        },
        {
          "defaultValue": null,
          "enums": null,
          "field": "value@@@kind",
          "integration": null,
          "inverseOf": null,
          "isFilterable": true,
          "isPrimaryKey": false,
          "isReadOnly": false,
          "isRequired": false,
          "isSortable": true,
          "isVirtual": false,
          "reference": null,
          "type": "String",
          "validations": []
        },
        {
          "defaultValue": null,
          "enums": null,
          "field": "value@@@lang",
          "integration": null,
          "inverseOf": null,
          "isFilterable": true,
          "isPrimaryKey": false,
          "isReadOnly": false,
          "isRequired": false,
          "isSortable": true,
          "isVirtual": false,
          "reference": null,
          "type": "String",
          "validations": []
        }
      ],
      "icon": null,
      "integration": null,
      "isReadOnly": false,
      "isSearchable": true,
      "isVirtual": false,
      "name": "lawyers_languages",
      "onlyForRelationships": false,
      "paginationType": "page",
      "segments": []
    },
...

```

## Observed behavior

Creation of new lawyer language in the UI leads to an error notification, but the operation is successful, if I refresh.

## Expected behavior

No error notification.

## Failure Logs

```javascript
Cannot read properties of undefined (reading 'type') 

TypeError: Cannot read properties of undefined (reading 'type')
    at BinaryCollectionDecorator.convertValue (/Users/davidroegiers/Jurata/jurata-next/forest-2/node_modules/@forestadmin/datasource-customizer/src/decorators/binary/collection.ts:182:16)
    at /Users/davidroegiers/Jurata/jurata-next/forest-2/node_modules/@forestadmin/datasource-customizer/src/decorators/binary/collection.ts:142:20
    at Array.map (<anonymous>)
    at BinaryCollectionDecorator.convertRecord (/Users/davidroegiers/Jurata/jurata-next/forest-2/node_modules/@forestadmin/datasource-customizer/src/decorators/binary/collection.ts:140:46)
    at /Users/davidroegiers/Jurata/jurata-next/forest-2/node_modules/@forestadmin/datasource-customizer/src/decorators/binary/collection.ts:102:61
    at Array.map (<anonymous>)
    at BinaryCollectionDecorator.create (/Users/davidroegiers/Jurata/jurata-next/forest-2/node_modules/@forestadmin/datasource-customizer/src/decorators/binary/collection.ts:102:42)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async PublicationFieldCollectionDecorator.create (/Users/davidroegiers/Jurata/jurata-next/forest-2/node_modules/@forestadmin/datasource-customizer/src/decorators/publication-field/collection.ts:34:21)
    at async RenameFieldCollectionDecorator.create (/Users/davidroegiers/Jurata/jurata-next/forest-2/node_modules/@forestadmin/datasource-customizer/src/decorators/rename-field/collection.ts:101:24)

```

## Context

- Project name: jurata-2
- Team name: jurata
- Environment name: staging
- Agent (forest package) name & version: “node\_modules/@forestadmin/agent”: {  
“version”: “1.13.1”,
- Database type: mongoose
- Recent changes made on your end if any: unknown, it used to work.

---

<div class="post-metadata">

### Author: ![David\_Roegiers](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/david_roegiers/32/1039_2.png) [@David\_Roegiers](https://community.forestadmin.com/u/David_Roegiers)
#### Post date: [July 10, 2023, 2:49pm UTC](https://community.forestadmin.com/t/bug-report-with-smart-models-on-node-js-agent/6480/2 "2023-07-10T14:49:41Z")

</div>

This is not finding the schema for the ‘value@@@kind’ field.

[https://github.com/ForestAdmin/agent-nodejs/blame/045a6c55da1e8697c527e52f815f8cb79d8cce25/packages/datasource-customizer/src/decorators/binary/collection.ts#L180](https://github.com/ForestAdmin/agent-nodejs/blame/045a6c55da1e8697c527e52f815f8cb79d8cce25/packages/datasource-customizer/src/decorators/binary/collection.ts#L180)

---

<div class="post-metadata">

### Author: ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)
#### Post date: [July 10, 2023, 3:00pm UTC](https://community.forestadmin.com/t/bug-report-with-smart-models-on-node-js-agent/6480/4 "2023-07-10T15:00:15Z")

</div>

This is clearly a bug, but let’s try to work around it.

Can you try the following config?

```javascript
agent.addDataSource(
  createMongooseDataSource(connection, {
    flattenMode: 'manual',
    flattenOptions: {
      offices: { asFields: ['address', 'postbox'], asModels: ['lawyers'] },
      lawyers: {
        asFields: ['billing', 'languages.value.kind', 'languages.value.lang', 'experience', 'education', 'practiceAreas', 'publications'],
        // ^-- I used the fully expanded syntax here
        asModels: ['languages', 'experience', 'education', 'practiceAreas', 'publications'],
      },
    },
  })
);

```

---

<div class="post-metadata">

### Author: ![David\_Roegiers](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/david_roegiers/32/1039_2.png) [@David\_Roegiers](https://community.forestadmin.com/u/David_Roegiers)
#### Post date: [July 10, 2023, 3:08pm UTC](https://community.forestadmin.com/t/bug-report-with-smart-models-on-node-js-agent/6480/5 "2023-07-10T15:08:06Z")

</div>

@anon39940173 Unfortunately, I get the same error on creation.

---

<div class="post-metadata">

### Author: ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)
#### Post date: [July 10, 2023, 3:36pm UTC](https://community.forestadmin.com/t/bug-report-with-smart-models-on-node-js-agent/6480/6 "2023-07-10T15:36:05Z")

</div>

I’ll make a PR (I have the bugfix working on my machine)

Thank you for filling out the bug report (and sorry for the issue…)

---

<div class="post-metadata">

### Author: ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)
#### Post date: [July 10, 2023, 4:00pm UTC](https://community.forestadmin.com/t/bug-report-with-smart-models-on-node-js-agent/6480/7 "2023-07-10T16:00:16Z")

</div>

> <https://github.com/ForestAdmin/agent-nodejs/pull/763>
>
> \## Definition of Done
> 
> \### General
> 
> \- \[\] Write an explicit title for the Pu…ll Request, following \[Conventional Commits specification\](https://www.conventionalcommits.org)
> \- \[\] Test manually the implemented changes
> \- \[\] Validate the code quality (indentation, syntax, style, simplicity, readability)
> 
> \### Security
> 
> \- \[\] Consider the security impact of the changes made

This should solve the issue.  
Hopefully, it should be merged soon.

Sorry for the inconvenience, and thanks again for the well-documented bug report.

---

<div class="post-metadata">

### Author: ![David\_Roegiers](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/david_roegiers/32/1039_2.png) [@David\_Roegiers](https://community.forestadmin.com/u/David_Roegiers)
#### Post date: [July 10, 2023, 4:57pm UTC](https://community.forestadmin.com/t/bug-report-with-smart-models-on-node-js-agent/6480/9 "2023-07-10T16:57:33Z")

</div>

Thanks for the quick reply and quick fix @anon39940173 👍
