SoftwareBrothers / adminjs

AdminJS is an admin panel for apps written in node.js
https://adminjs.co
MIT License
8.19k stars 662 forks source link

[Bug]: Error: Objects are not valid as a React child #1578

Closed rlabrovi closed 11 months ago

rlabrovi commented 11 months ago

Contact Details

my github account

What happened?

Error: Objects are not valid as a React child (found: object with keys {en, it, fr, de}). If you meant to render a collection of children, use an array instead. See development console for more details...

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { ObjectId } from 'mongoose';

export class Language {
  en: string;
  it: string;
  fr: string;
  de: string;
}

export class Option {
  _id: ObjectId;
  title: string;
  correct: boolean;
}

@Schema({ timestamps: true })
export class Question {
  _id: ObjectId;

  @Prop({ type: Language, required: true })
  title: Language;

  @Prop({ type: Language })
  funFact: Language;

  @Prop({ type: Option, required: true })
  optionsEn: Option[];

  @Prop({ type: Option, required: true })
  optionsIt: Option[];

  @Prop({ type: Option, required: true })
  optionsFr: Option[];

  @Prop({ type: Option, required: true })
  optionsDe: Option[];
}

export const QuestionSchema = SchemaFactory.createForClass(Question);
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument, Types } from 'mongoose';

import { Prize } from 'src/prize/entities/prize.entity';

import { Question } from '../../questions/entities/question.entity';

export type QuizDocument = HydratedDocument<Quiz>;

@Schema({ timestamps: true })
export class Quiz {
  @Prop()
  day: string;

  @Prop({ type: [{ type: Types.ObjectId, ref: Question.name }] })
  questions: Question[];

  @Prop({ type: [{ type: Types.ObjectId, ref: Prize.name }] })
  prizes: Prize[];

  @Prop({ type: [{ type: Types.ObjectId }] })
  winners: any[];

  @Prop()
  numberOfWinners: number;

  @Prop()
  quizStart: Date;

  @Prop()
  quizEnd: Date;
}

export const QuizSchema = SchemaFactory.createForClass(Quiz);

Bug prevalence

Whenever I try to assign question to quiz

AdminJS dependencies version

"dependencies": { "@adminjs/express": "^5.0.1", "@adminjs/mongoose": "^3.0.1", "@adminjs/nestjs": "^5.1.0", "@adminjs/upload": "^3.0.1", "@nestjs/common": "9.2.1", "@nestjs/core": "9.2.1", "@nestjs/mongoose": "^9.2.1", "@nestjs/platform-express": "9.2.1", "@nestjs/swagger": "^6.3.0", "@nestjs/terminus": "^10.1.1", "adminjs": "^6.7.5", "class-transformer": "0.5.1", "class-validator": "^0.14.0", "dotenv": "^16.3.1", "express-formidable": "^1.2.0", "express-session": "^1.17.3", "mongoose": "^6.8.3", "reflect-metadata": "0.1.13", "rxjs": "7.5.5" }, "devDependencies": { "@nestjs/cli": "^9.5.0", "@nestjs/config": "^3.1.1", "@nestjs/schematics": "9.0.4", "@trivago/prettier-plugin-sort-imports": "^4.2.1", "@types/express": "4.17.13", "@types/node": "18.0.0", "@types/supertest": "2.0.12", "@typescript-eslint/eslint-plugin": "^5.0.0", "@typescript-eslint/parser": "^5.0.0", "eslint": "8.18.0", "eslint-config-prettier": "^8.3.0", "eslint-plugin-prettier": "^4.0.0", "prettier": "^2.8.8", "source-map-support": "^0.5.20", "supertest": "6.2.3", "ts-loader": "9.3.0", "ts-node": "10.8.1", "tsconfig-paths": "3.14.1", "typescript": "4.7.4" },

What browsers do you see the problem on?

No response

Relevant log output

No response

Relevant code that's giving you issues

No response

rlabrovi commented 11 months ago

I was able to fix this by adding one more property called 'name' to entity and then when saving question resource saving title.en to name property:

const setName = (request, content) => {
  const { payload } = request;
  const titleEn = payload['title.en'];

  if (titleEn) {
    payload.name = titleEn;
  }

  return request;
};
    actions: {
      new: {
        before: setName,
      },
      edit: {
        before: setName,
      },
    },