adminjs icon indicating copy to clipboard operation
adminjs copied to clipboard

Mongoose nested schemas not showing _id field

Open maxiejbe opened this issue 2 years ago • 4 comments

Describe the bug When showing/editing a parent resource (mongoose model) with an array of nested schemas, each of the schemas is not showing the _id field.

Installed libraries and their versions

To Reproduce Steps to reproduce the behavior:

  1. Go to resource list
  2. Click on show in any resource
  3. Go to nested schemas list
  4. See no _id field in nested schemas

Expected behavior Each of the nested schemas should have the _id field.

Screenshots

image

AdminJSOptions with schema

Default adminJs options

const adminJsOptions = {
    resources: [ Thematic ]
};

thematic.js

export const ThematicSchema = {
  subthematics: [ SubthematicsSchema ]
};

export const Thematic = mongoose.model('Thematic', ThematicSchema);

subthematic.js

export default SubthematicSchema = new Schema(
  {
    title: String,
  },
  // This is the default behavior, but just to indicate that each nested schema could have an _id
  { _id: true, timestamp: true }
);

maxiejbe avatar Jun 26 '22 02:06 maxiejbe

Can you try:

const adminJsOptions = {
    resources: [{
        resource: Thematic,
        options: {
            properties: {
                'subthematics._id': { type: 'string' },
            }
        }
    }]
};

It looks like it isn't automatically detected as a field

dziraf avatar Jun 27 '22 11:06 dziraf

@dziraf I've already tried that and unfortunately it isn't working. Is it for you?

In addition, each of the subthematics have a 2nd level nested schema (questions). In case that works at the end, would it be possible to add something like:

const adminJsOptions = {
    resources: [{
        resource: Thematic,
        options: {
            properties: {
                'subthematics._id': { type: 'string' },
                'subthematics.questions._id': { type: 'string' },
            }
        }
    }]
};

maxiejbe avatar Jun 27 '22 11:06 maxiejbe

You can also try to additionally add:

subthematics: { type: 'mixed', isArray: true }

it's difficult to say why this happens. @adminjs/mongoose is the only adapter which is able to take automatically create subproperties from subschemas, for all other adapters you have to manually define them like we did above. Also please check if _id is actually present in server response by going to Network tab in your dev tools.

dziraf avatar Jun 27 '22 11:06 dziraf

@dziraf Sorry, still not working 😞 Just double checked that the record has a field like "subthematics.0._id":"61a909b82eee2300146121c9" for each subschema.

maxiejbe avatar Jun 27 '22 12:06 maxiejbe