MongooseError - Operation buffering timed out after 10000ms

1.9k Views Asked by At

I have following code for model.

import { DatabaseServer } from './database-server';
import { ProjectGroup } from './project-group';
import { ProjectUser } from './project-user';
import { prop, getModelForClass, ReturnModelType, modelOptions } from '@typegoose/typegoose';
import { defaultTransform, ModelBase } from '../general/model-base';
import { ObjectID } from 'mongodb';
import { Connection } from 'mongoose';
import { BeAnObject } from '@typegoose/typegoose/lib/types';

export class Datasource extends ModelBase {

  @prop()
  databaseServer?: DatabaseServer;
  @prop()
  databaseServerId?: ObjectID;
  @prop()
  datasource?: Datasource[];

  @prop()
  name?: string;
  @prop()
  projectGroups?: ProjectGroup[];
  @prop()
  projectUsers?: ProjectUser[];

}

const DatasourceModel = (
  connection: Connection,
): ReturnModelType<typeof Datasource, BeAnObject> => {
  return getModelForClass(Datasource, {
    ...defaultTransform,
    ...{
      existingConnection: connection,
    },
  });

};
export { DatasourceModel };

And I am using above model as following.

await DatasourceModel(await this.masterContext).find({})

Where mastercontext is defined as below.

import {
  Connection,
  createConnection
} from 'mongoose';

export class MasterContext {
  get context(): Promise<Connection> {
    if (!this.m_context) {
      this.m_context = createConnection('mongodb://localhost/Master', {
        useNewUrlParser: true,
        useUnifiedTopology: true,
      });
    }

    return this.m_context;
  }
  private m_context: Promise<Connection>;
}

I am getting error as following.

Operation datasources.find() buffering timed out after 10000m

If I change class name from export class Datasource to any other name (e.g. export class Datumsource) then error is not throwing.

So is Datasource reserved keyword in MongoDb or Mongoose or Typegoose?

1

There are 1 best solutions below

2
On

from what i know, this error means that the connection is not connected, so the command (find) has an timeout

i also would recommend to either cache the DatasourceModel or only run the function once (the connection does not need to be connected to create an model, it only needs to be connected to do commands (like find))
so if you have an global connection, you should simply remove the function and just run getModelForClass, but if you have an "local" connection (like from an class property), then you should cache it there, example:

// i think this way of defining stuff is common in nestjs?
class Dummy {
  public connection: mongoose.Connection;
  public model: mongoose.Model;

  constructor(connection: mongoose.Connection) {
    this.connection = connection;
    this.model = getModelForClass({ ...otherGlobalStuff, existingConnection: connection });
    // or when wanting to use your function
    this.model = DatasourceModel(connection);
  }
}

// and if for one file only
let model = DatasourceModel(connection);

Some other notes:

  • if you want to use arrays in typegoose, you need to manually define the type with the type option, look here on why
  • only the mongoose.Types.ObjectId type should be used for an ObjectId