首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MongooseError -操作缓冲在10000ms后超时

MongooseError -操作缓冲在10000ms后超时
EN

Stack Overflow用户
提问于 2021-06-05 19:42:50
回答 1查看 491关注 0票数 2

我有以下模型代码。

代码语言:javascript
复制
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 };

我使用上面的模型,如下所示。

代码语言:javascript
复制
await DatasourceModel(await this.masterContext).find({})

其中,mastercontext的定义如下。

代码语言:javascript
复制
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),则不会抛出错误。

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

EN

回答 1

Stack Overflow用户

发布于 2021-06-10 17:30:41

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

我还建议要么缓存DatasourceModel,要么只运行函数一次(创建模型时不需要连接连接,只需连接执行命令(如find))

因此,如果你有一个全局连接,你应该简单地删除函数并运行getModelForClass,但是如果你有一个“本地”连接(比如来自一个类属性),那么你应该把它缓存在那里,例如:

代码语言:javascript
复制
// 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);

其他一些注意事项:

如果要在ObjectId中使用数组,则需要使用type选项手动定义类型,mongoose.Types.ObjectId类型应用于typegoose

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67849200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档