midwayjs / midway

🍔 A Node.js Serverless Framework for front-end/full-stack developers. Build the application for next decade. Works on AWS, Alibaba Cloud, Tencent Cloud and traditional VM/Container. Super easy integrate with React and Vue. 🌈
https://www.midwayjs.org/
MIT License
7.4k stars 577 forks source link

@midwayjs/typegoose 报Operation `*.findOne()` buffering timed out after 10000ms #2374

Closed wgcairui closed 2 years ago

wgcairui commented 2 years ago

当我有一个default默认数据库配置,mongoose.connections中有两个连接,此时任何请求操作都导致Operation *.findOne() buffering timed out after 10000ms 超时,但是我把mongoose.connections[0]中的连接配置调整为default之后,工作都正常了,我从midway V2版本更新上来的,是不是哪里没有配置? 之前core@3.1.6版本还好的

config.default.ts配置

export default {
  mongoose: {
    dataSource: {
      default: {
        uri: `mongodb://${
          process.env.NODE_Docker === 'docker' ? 'mongo' : '127.0.0.1'
        }:27017/test`,
        options: {
          useNewUrlParser: true,
          useUnifiedTopology: true,
        } as ConnectOptions,
        // 关联实体
        entities: ['**/entity/*.ts'],
      },
    },
  },
}

我的补救措施

import { Autoload, Init, Provide, Scope, ScopeEnum } from '@midwayjs/decorator';
import mongoose from 'mongoose';
import config from '../config/config.default';

@Provide()
@Scope(ScopeEnum.Singleton)
@Autoload()
export class MongooseDataSource {
  @Init()
  async init() {
    await mongoose.connect(
      config.mongoose.dataSource.default.uri,
      config.mongoose.dataSource.default.options
    );
  }
}
czy88840616 commented 2 years ago
image

这个删了么,把 entity 也贴一下。

此外, entities 不需要写后缀,entities: ['**/entity/']

wgcairui commented 2 years ago

没有使用EntityModel

image

entity

import { modelOptions, prop, Ref } from '@typegoose/typegoose';
import { Schema } from 'mongoose';

class Devprotocol {
  @prop({ enum: [485, 232], default: 485 })
  public Type: number;

  @prop()
  public Protocol: string;
}

/**
 * 设备信息
 */
@modelOptions({ schemaOptions: { collection: 'device.types' } })
export class DevType {
  @prop()
  public Type!: string;

  @prop()
  public DevModel!: string;

  @prop({ type: () => Devprotocol })
  public Protocols: Devprotocol[];
}

// 关联实体 entities: ['**/entity'] 后戳改了也一样

czy88840616 commented 2 years ago

@wgcairui 给我一个可复现的最小 demo ?

wgcairui commented 2 years ago

不好弄,我连了五六个服务,不好拆分,先这样用吧,我mongo也没有多库的需求,后面自己新建一个v3的demo看看

github-actions[bot] commented 1 year ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.