SoftwareBrothers / adminjs

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

Nest.js mongoose schema resource error #1257

Open velidan opened 2 years ago

velidan commented 2 years ago

Describe the issue When I try to use the mongoose schema from the official Nest.js documentation Mongo with Mongoose as a resource for the Admin.js the error occurs and the Admin.js doesn't work. The error:

UnhandledPromiseRejectionWarning: TypeError: Cannot read property '
paths' of undefined
    at Resource.properties (E:\web\trading\trade-auto-fly\backend\node_modules\@
adminjs\mongoose\src\resource.ts:60:55)

Installed libraries and their versions

    "@adminjs/express": "^5.0.0",
    "@adminjs/mongoose": "^3.0.0",
    "@adminjs/nestjs": "^5.0.1",
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/mongoose": "^9.2.0",
    "@nestjs/platform-express": "^9.0.0",
    "adminjs": "^6.2.3",
    "express-formidable": "^1.2.0",
    "express-session": "^1.17.3",
    "mongoose": "^6.6.1",
    "nestjs-admin": "^0.4.1",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/config": "^2.2.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/express": "^4.17.13",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.1.0",
    "typescript": "^4.7.4"
  },

To Reproduce Steps to reproduce the behavior:

  1. Install Nest.js (latest), install Mongo/mongoose, and all the required Admin.js packages.

  2. Create a Nest.js Mongoose schema as it described in the official documentation Mongo with Mongoose

  3. Import the Mongoose Schema and use it as a Resource in Admin.js

  4. See the error

Expected behavior Admin.js should work fine Screenshots creating the Nest.js schema and using it as a resource for Admin.js: admin_js_demo the Error: admin_js_error The Nest.js Mongo documentation page: nest mongo

AdminJSOptions with schema

AdminModule.createAdminAsync({
      useFactory: () => ({
        adminJsOptions: {
          rootPath: '/admin',
          resources: [OrderSchema],
        },
        auth: {
          authenticate,
          cookieName: 'adminjs',
          cookiePassword: 'secret',
        },
        sessionOptions: {
          resave: true,
          saveUninitialized: true,
          secret: 'secret',

        },
      }),
    }),

Desktop (please complete the following information if relevant):

Additional context I'm wondering how to use the Nest.js mongoose implementation with the Admin.js. It looks like the Admin.js documentation is missing this chapter because the Entity way that is described in the Admin.js documentation can't be applied for the latest Nest.js mongoose approach.

Thanks for any help!

dziraf commented 2 years ago

I don't really use Nest nor Mongoose much, but have you tried to pass Order instead of OrderSchema into resources? Unless @nestjs/mongoose schema is different than mongoose schema. Our current documentation (https://docs.adminjs.co/installation/adapters/mongoose) and demo app doesn't use @nestjs/mongoose

velidan commented 2 years ago

Hi @dziraf , thank you for your feedback. Yes, I tried to use the Order but it also doesn't work. Probably it's a bit different than the mongoose native stuff. So, if I understand correctly, if I want to use the Admin.js with Nest.js I shouldn't use their mongoose package but rather use the native mongoose schema, etc. ?

dziraf commented 2 years ago

I'm 99.9% certain that with native mongoose package and schema it will work. Still, if it doesn't work with @nestjs/mongoose that isn't good too. It's strange though as other @nestjs/package work fine. If it's possible can you prepare a repository where I can reproduce this so I can prepare a fix?

The line which fails is: https://github.com/SoftwareBrothers/adminjs-mongoose/blob/master/src/resource.ts#L60 I'd assume that in your case this.MongooseModel is actually a schema so it doesn't have .schema property.

duongapollo commented 1 year ago

i had the same issue, the console said: "cannot read property 'paths' of undefined"

velidan commented 1 year ago

@dziraf Hi, I've tried to create a single dedicated repo with the minimal amount of code that contains the issue, hope it helps. If you need some help, please, let me know: https://github.com/velidan/adminjs-nestjs-mongoose-issue

to reproduce an issue: 1) install node_modules 2) run the mongod (mongo server) 3) stand the nest.js app (yarn start:dev) 4) try to open the Admin.js page http://localhost:3000/admin

Kind regards!

kusaljr commented 1 year ago

Is there any fix for this?

Georgemalima commented 1 year ago

any fix for this?

junguksim commented 1 year ago

Any fix for this?