I'm attempting to integrate AdminJS with Nest.js.
I can successfully log in to the /admin panel, but when I attempt to access a specific resource, I encounter the following error: "MongooseError: Operation buffering timed out after 10000ms."
Here's a snippet of my package.json file showing the relevant dependencies:
"dependencies": {
"@adminjs/express": "^6.0.1",
"@adminjs/mongoose": "^4.0.0",
"@adminjs/nestjs": "^6.0.1",
"@nestjs/common": "^9.0.0",
"@nestjs/core": "^9.0.0",
"@nestjs/mongoose": "^10.0.1",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/swagger": "^7.1.13",
"adminjs": "^7.3.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"express-formidable": "^1.2.0",
"express-session": "^1.17.3",
"mongoose": "^7.6.3",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.2.0"
}
And here's a section of my app.module.ts file:
(async () => {
const AdminJSMongoose = await import('@adminjs/mongoose');
const AdminJS = (await import('adminjs')).AdminJS;
AdminJS.registerAdapter({
Resource: AdminJSMongoose.Resource,
Database: AdminJSMongoose.Database,
});
})();
const DEFAULT_ADMIN = {
email: '[email protected]',
password: 'password',
};
const authenticate = async (email: string, password: string) => {
if (email === DEFAULT_ADMIN.email && password === DEFAULT_ADMIN.password) {
return Promise.resolve(DEFAULT_ADMIN);
}
return null;
};
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
}),
// Mongoose connection options: https://mongoosejs.com/docs/connections.html
MongooseModule.forRoot(process.env.MONGODB_URL),
// AdminJS version 7 is ESM-only. In order to import it, you have to use dynamic imports.
import('@adminjs/nestjs').then(({ AdminModule }) =>
AdminModule.createAdminAsync({
useFactory: () => ({
adminJsOptions: {
rootPath: '/admin',
resources: [
{
resource: model('Question', QuestionSchema),
options: {
navigation: {
name: 'Sunrise',
},
properties: {
id: { type: 'string' },
text: { type: 'richtext' },
options: { type: 'mixed' },
correctOptionId: { type: 'string' },
image: { type: 'file' },
language: { type: 'string' },
},
},
},
],
},
auth: {
authenticate,
cookieName: 'adminjs',
cookiePassword: 'secret',
},
sessionOptions: {
resave: true,
saveUninitialized: true,
secret: 'secret',
},
}),
}),
),
QuestionsModule,
],
controllers: [AppController],
providers: [
AppService,
{ provide: APP_FILTER, useClass: AllExceptionsFilter },
{ provide: APP_FILTER, useClass: HttpExceptionFilter },
{ provide: APP_FILTER, useClass: ValidationFilter },
],
})
Your database is not connecting...
Specifically the line:
MongooseModule.forRoot(process.env.MONGODB_URL),
Initializes a module for mongoose to connect to the mongodb instance over the network at MONGODB_URL which should be specified as an environment variable.
Are you using something like dotenv?
If so, you need MONGODB_URL to be defined in your .env
file
If not, you need to somehow provide a mongodb url to that "forRoot" function I mentioned before.
If you do not yet have a mongodb database instance running locally or hosted over the internet, I recommend MongoDB Atlas. (I am not associated in any way it's just what I use because they have a powerful free tier.)