SoftwareBrothers / adminjs

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

Error when adding resource: Cannot read property 'split' of undefined #879

Closed ghost closed 3 years ago

ghost commented 3 years ago

https://i.imgur.com/Ykg1FSX.png

Without the resource, it works fine, but when adding a resource it throws this error.

import mongoose from 'mongoose'
import express from 'express'
import AdminJS from 'adminjs'
import AdminJSMongoose from '@adminjs/mongoose'
import AdminJSExpress from '@adminjs/express'
import { User } from '../../back/src/Models'

AdminJS.registerAdapter(AdminJSMongoose)

const run = async () => {
    const mongooseDb = await mongoose.connect(
        'mongodb://localhost:27017/db',
        { useNewUrlParser: true }
    )

    const app = express()

    const adminJs = new AdminJS({
        resources: [
            {
                resource: User,
                options: {
                    // ...your options go here
                }
            }
        ],
        branding: {
            companyName: '...'
        },
        rootPath: '/admin'
    })

    const router = AdminJSExpress.buildRouter(adminJs)

    app.use(adminJs.options.rootPath, router)

    app.listen(1332, () => console.log('server started'))
}

run()

../../back/src/Models

import mongoose, { Schema, Document } from 'mongoose'

export interface IUser extends Document {
    email: string
    passwordHash: string
    authToken: string
    knownIPs: string[]
    isAdmin: boolean
}

export const UserSchema: Schema = new Schema({
    email: { type: String, required: true, unique: true },
    passwordHash: { type: String, required: true },
    authToken: { type: String, required: true },
    knownIPs: {
        type: [String],
        required: true,
        default: []
    },
    isAdmin: { type: Boolean, required: true }
})

export const User = mongoose.model<IUser>('User', UserSchema)

This throws the error. If the resources option is just [], it runs without problems. I think it's a problem with my model or database.

Any help would be greatly appreciated. Thanks

ghost commented 3 years ago

Nevermind, it was a problem with my mongoose installation. I noticed that I couldn't even do db operations manually, somehow the node_modules were installed in the workspace folder instead of the main one. Works well now.