adminjs
adminjs copied to clipboard
Mongoose nested schemas not showing _id field
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
- [email protected]
- @adminjs/[email protected]
- @adminjs/[email protected]
- [email protected]
- [email protected]
To Reproduce Steps to reproduce the behavior:
- Go to resource list
- Click on show in any resource
- Go to nested schemas list
- See no _id field in nested schemas
Expected behavior Each of the nested schemas should have the _id field.
Screenshots
![image](https://user-images.githubusercontent.com/5941817/175796585-e4e91ae7-1349-4156-aa9c-46fb110bd945.png)
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 }
);
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 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' },
}
}
}]
};
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 Sorry, still not working 😞 Just double checked that the record has a field like "subthematics.0._id":"61a909b82eee2300146121c9"
for each subschema.