Search code examples
node.jsmongoosenestjsadminjs

Adminjs cant fetch data from mongo database. (NestJS)


I am using adminjs in nestjs and mongoose

this is my product model const ProductModel = model<IProduct>('product', ProductSchema);

this is my resource

resources: [
      {
        resource: ProductModel,
        options: { 
            navigation: { name: null }, 
            show: {
            before: async (request, response, context) => {
              throw new Error(
                'Data fetching has been disabled for this resource',
              );
            },
            handler: async (request, response, context) => {
              return {
                records: [],
              };
            },
          },
          list: {
            before: async (request, response, context) => {
              throw new Error(
                'Data fetching has been disabled for this resource',
              );
            },
            handler: async (request, response, context) => {
              return {
                records: [],
              };
            },
          },
      },
]```




this is error i get 

[Nest] 23592 - 04/12/2023, 11:16:57 AM ERROR [ExceptionsHandler] Operation products.find() buffering timed out after 10000ms MongooseError: Operation products.find() buffering timed out after 10000ms at Timeout. (C:\Users\user\Documents\backend\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:185:23) at listOnTimeout (node:internal/timers:559:17) at processTimers (node:internal/timers:502:7)


how to resolve this? 

Solution

  • I have resolved the Error.

    I made adminjs module wait 5 seconds before initialize:

    import { AdminModule as AdminJsModule } from '@adminjs/nestjs';
    import { adminModuleOptions } from './admin-module.options';
    
    AdminJsModule.createAdminAsync({
        useFactory: async () => {
          await new Promise((resolve) => setTimeout(resolve, 5000));
          return adminModuleOptions;
        },
      }),
    

    Then i made mongoose module wait 5 seconds before connecting:

    MongooseModule.forRootAsync({
        useFactory: async (): Promise<MongooseModuleOptions> => ({
          uri: mongoCredentials.url,
          connectionFactory: () => {
            const connection = mongoose.connection;
            setTimeout(() => mongoose.connect(mongoCredentials.url), 5000);
            return connection;
          },
        }),
      }),