Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to create new records #18

Open
mechaadi opened this issue May 31, 2021 · 4 comments
Open

Not able to create new records #18

mechaadi opened this issue May 31, 2021 · 4 comments

Comments

@mechaadi
Copy link

I am not able to save anything via the admin bro admin panel, I am able to read the data from all the available tables but not able to write anything to any table. Here are the network logs

image

The save button just keeps on showing the loading animation, it was working fine before but I had to create some more tables again and it stopped working, and yes, I have deleted the whole database and created a fresh one just in case, but no luck 😕

Here's the code of admin bro builder function

import { Database, Resource } from '@admin-bro/typeorm'
const AdminBro = require('admin-bro')
const AdminBroExpress = require('@admin-bro/express')
import { validate } from 'class-validator'
import { Admin } from "../entity/Admin";
const bcrypt = require('bcrypt');

AdminBro.registerAdapter({ Database, Resource });
Resource.validate = validate

const setupAdminBro = (connection) => {
    return new Promise(resovle => {
        const adminBro = new AdminBro({
            resources: [
                {
                    resource: Admin,
                    options: {
                        properties: {
                            encryptedPassword: {
                                isVisible: false,
                            },
                            password: {
                                type: 'string',
                                isVisible: {
                                    list: false, edit: true, filter: false, show: false,
                                },
                            },
                        },
                        actions: {
                            new: {
                                before: async (request) => {
                                    console.log(request)
                                    if (request.payload.password) {
                                        request.payload = {
                                            ...request.payload,
                                            encryptedPassword: await bcrypt.hash(request.payload.password, 10),
                                            password: undefined,
                                        }
                                    }
                                    return request
                                },
                            }
                        }
                    }
                }
            ],
            databases: [connection],
            rootPath: '/admin',
        })

        //authenticated view
        // const router = AdminBroExpress.buildAuthenticatedRouter(adminBro, {
        //     authenticate: async (email, password) => {
        //         const admin = await Admin.findOne({
        //             where: { username: email }
        //         })
        //         if (admin) {
        //             const matched = await bcrypt.compare(password, admin.encryptedPassword)
        //             if (matched) {
        //                 return admin
        //             }
        //         }
        //         return false
        //     },
        //     cookiePassword: 'some-secret-password-used-to-secure-cookie',
        // })

        //unauth view
        const router = AdminBroExpress.buildRouter(adminBro)
        resovle(router);
    })
}

module.exports = { setupAdminBro };

And here's the driver function which calls this function

const adminRouter = await setupAdminBro(connection)
console.log("Admin panel is up!")
app.use('/admin', adminRouter)

Please help!

@Compound-Z
Copy link

i have the same problem, have u managed to solve it?

@mechaadi
Copy link
Author

Unfortunately not, i gave up on it and ended up creating my own admin panel

@amanzrx4
Copy link

hey @mechaadi you need to register mongoose adapter for working with db
AdminJS.registerAdapter(AdminJSMongoose);

@mechaadi
Copy link
Author

@amanzrx4, I was not using mongodb as my database, I was using postgre and hence used typeORM to create a connection with the following code:

import { Database, Resource } from '@admin-bro/typeorm'
AdminBro.registerAdapter({ Database, Resource });

So, the database adapter was configured properly, making it possible to run in the first run, but the issue came when I created new tables (I have also tried purging the whole DB and using a fresh one, same problem).

I used this wrapper around typeORM for admin-bro: https://www.npmjs.com/package/@admin-bro/typeorm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants