【问题标题】:MongooseError - Operation buffering timed out after 10000msMongooseError - 操作缓冲在 10000 毫秒后超时
【发布时间】:2021-08-23 05:08:48
【问题描述】:

我有以下模型代码。

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 };

我正在使用上述模型如下。

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

mastercontext 的定义如下。

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>;
}

我收到如下错误。

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

如果我将类名从 export class Datasource 更改为任何其他名称(例如 export class Datumsource),则不会引发错误。

那么 Datasource 在 MongoDb 或 Mongoose 或 Typegoose 中是保留关键字吗?

【问题讨论】:

  • 至少在typegoose中没有保留
  • 你可以包括使用的版本(typegoose、mongoose、typescript)和编译器(tsc / babel / ts-node)

标签: node.js typescript mongodb mongoose typegoose


【解决方案1】:

据我所知,这个错误意味着连接没有连接,所以命令(find)有超时

我也建议缓存DatasourceModel或只运行一次函数(创建模型不需要连接连接,只需要连接执行命令(如find))
所以如果你有一个全局连接,你应该简单地删除这个函数并运行getModelForClass,但是如果你有一个“本地”连接(比如来自一个类属性),那么你应该把它缓存在那里,例如:

// 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);

其他一些注意事项:

  • 如果你想在typegoose中使用数组,你需要用type选项手动定义类型,look here on why
  • 只有mongoose.Types.ObjectId 类型应该用于ObjectId

【讨论】:

  • 不知何故。当我更改模型名称时,它正在工作。所以连接没有问题,因为它正在使用 DatasourceS 模型,并且在 DatasourceModel 用于查找之前等待,而不是模型本身。
  • 那么抱歉,我不知道 - 至少在 typegoose 中,名称 Datasource(任何情况下)未被保留或使用
猜你喜欢
  • 2021-11-15
  • 2021-12-08
  • 2021-04-01
  • 2021-06-30
  • 2021-04-01
  • 2021-04-12
  • 2021-04-11
  • 1970-01-01
  • 2022-11-28
相关资源
最近更新 更多