Bug report with Smart Models on Node.js agent

Feature(s) impacted

Model:

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

configuration

...
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:

...
    {
      "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

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.

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

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

Can you try the following config?

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'],
      },
    },
  })
);

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

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…)

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

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

Thanks for the quick reply and quick fix @anon39940173 :+1: