adminjs icon indicating copy to clipboard operation
adminjs copied to clipboard

Object's values in array of objects aren't displayed

Open petrakoww opened this issue 1 year ago • 10 comments

Describe the bug When I try to view a document with array of objects, the values aren't displayed, but everything in Mongo seems fine.

Installed libraries and their versions

To Reproduce Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior Display all values.

Screenshots bug

AdminJSOptions with schema Options

import AdminJS from "adminjs";
import AdminJSMongoose from "@adminjs/mongoose";
import { Order } from "../models/order.js";
AdminJS.registerAdapter(AdminJSMongoose);

export const adminOptions = new AdminJS({
  resources: [{ resource: Order }],
  rootPath: "/admin",
});

Mongo Schema

import mongoose from "mongoose";
const Schema = mongoose.Schema;

const orderSchema = new Schema(
  {
    test: { type: String },
    array: [
      {
        _id: false,
        material: {
          name: {
            type: String,
          },
          colors: [
            {
              _id: false,
              name: { type: String },
            },
          ],
        },
      },
    ],
  },
  { versionKey: false }
);

export const Order = mongoose.model("Order", orderSchema);

Desktop (please complete the following information if relevant):

  • OS: Windows 10
  • Browser Chrome
  • Version [e.g. 22]

Additional context Neither of name in material or name in the array of object is displayed.

petrakoww avatar Oct 03 '22 12:10 petrakoww

You either have to define every sub property with new Schema(...):

material: new Schema(...)

or describe properties in AdminJS resource options for your model - this is how it works for every other adapter/orm/odm:

properties: {
  array: {
    isArray: true,
    type: 'mixed',
  },
  'array.material': {
    type: 'mixed'
  },
  'array.material.name': {
    type: 'string'
  },
  'array.material.colors': {
    type: 'mixed',
    isArray: true,
  },
  'array.material.colors.name': {
    type: 'string',
  },
}

dziraf avatar Oct 04 '22 07:10 dziraf

Using the first solution works. But with the second I'm getting the fields but with no values.

image image

petrakoww avatar Oct 04 '22 08:10 petrakoww

Can you show how record.params look like in your Network response?

dziraf avatar Oct 04 '22 10:10 dziraf

"params":{
         "_id":"633bf47f72bb0e3f0e371d12",
         "array.0.material.name":"testName",
         "array.0.material.colors.0.name":"colorName",
         "test":"test"
      },

petrakoww avatar Oct 04 '22 10:10 petrakoww

This seems to be a correct response to me. Did you consider updating adminjs to the latest version? I think there were some fixes to mixed type throughout the last year or two.

dziraf avatar Oct 12 '22 07:10 dziraf

There has been a mistake in the description. My version is 6.2.4.

petrakoww avatar Oct 12 '22 11:10 petrakoww

I am using version 6.6.5 with the exactly same issue.

sourabh1983 avatar Nov 29 '22 05:11 sourabh1983

@sourabh1983 do you also use mongoose?

dziraf avatar Nov 29 '22 14:11 dziraf

@sourabh1983 do you also use mongoose?

I am using Prisma

below is response.record.params

{
  Id: '03b94717-5650-43bc-9e70-cde47d942f98',
  Name: "ICC U19 Men's Cricket World Cup",
  ShortName: 'ICC U19 M CWC',
  participatingTeams: [
    {
      Id: '0124b967-3a01-4ccb-8032-857e5d077604',
      CompetitionId: '03b94717-5650-43bc-9e70-cde47d942f98',
      TeamId: '7a5c8ec9-981c-4de7-bf53-2d39b48c2661',
      Name: 'Pakistan Under-19s'
    },
    {
      Id: '15d316cc-abbd-4b0b-9737-9a36f372ffdb',
      CompetitionId: '03b94717-5650-43bc-9e70-cde47d942f98',
      TeamId: '6557735a-b611-4dfd-9e66-52e9e184375b',
      Name: 'Papua New Guinea Under-19s'
    },
    ...
  ]
}

{
    resource: {model: dmmf.modelMap.Competitions, client: prismaClient},
    options: {
        navigation: {
        icon: 'Trophy'
    },
        properties: {
            participatingTeams: {
                isVisible: {
                    edit: false,
                    list: false,
                    filter: false,
                    show: true,
                },
                type: 'mixed',
                isArray: true,
            },
            'participatingTeams.Name': {
                type: 'string',
            },
        }
    }
}

Screen Shot 2022-11-30 at 9 44 36 am

sourabh1983 avatar Nov 29 '22 21:11 sourabh1983

How do you get participatingTeams? AdminJS uses a flat data model so if participatingTeams is structured as a nested json it won't work.

Your response should look like:

// other params
participatingTeams.0.Id: '0124b967-3a01-4ccb-8032-857e5d077604',
participatingTeams.0.CompetitionId: '03b94717-5650-43bc-9e70-cde47d942f98',
participatingTeams.0.TeamId: '7a5c8ec9-981c-4de7-bf53-2d39b48c2661',
participatingTeams.0.Name: 'Pakistan Under-19s',
participatingTeams.1.Id: '15d316cc-abbd-4b0b-9737-9a36f372ffdb',
participatingTeams.1.CompetitionId: '03b94717-5650-43bc-9e70-cde47d942f98',
participatingTeams.1.TeamId: '6557735a-b611-4dfd-9e66-52e9e184375b',
participatingTeams.1.Name: 'Papua New Guinea Under-19s'

dziraf avatar Nov 30 '22 10:11 dziraf