NestJS 和 Mongoose 使用 Mongoose-Sequence

Posted

技术标签:

【中文标题】NestJS 和 Mongoose 使用 Mongoose-Sequence【英文标题】:NestJS and Mongoose Using Mongoose-Sequence 【发布时间】:2020-11-11 04:01:51 【问题描述】:

我正在尝试在我的 NestJS 应用程序中使用插件“mongoose-sequence”。我希望这个插件可以被我将为这个应用程序创建的所有模块使用。这是我的 app.module.ts:

import  Module  from '@nestjs/common';
import  MongooseModule  from '@nestjs/mongoose';
import  UsersModule  from './user/user.module';
import AutoIncrementFactory from 'mongoose-sequence';

const mongoUri = 'mongodb://localhost/apptest';
const connName = 'apptest';

@Module(
  imports: [
    MongooseModule.forRoot(mongoUri, 
      connectionName: connName,
      useFindAndModify: false, 
      connectionFactory: (connection, connName) => 
        const AutoIncrement = AutoIncrementFactory(connection);
        connection.plugin(AutoIncrement);
        return connection;
      ),
      UserModule,
    ],
    controllers: [],
    providers: [],
);
export class AppModule 

当我启动服务器时,我收到以下错误消息:

Found 0 errors. Watching for file changes.

[Nest] 372   - 07/21/2020, 11:38:48 AM   [NestFactory] Starting Nest application...
[Nest] 372   - 07/21/2020, 11:38:48 AM   [MongooseModule] Unable to connect to the database. Retrying (1)... +35ms
[Nest] 372   - 07/21/2020, 11:38:48 AM   [InstanceLoader] MongooseModule dependencies initialized +1ms
[Nest] 372   - 07/21/2020, 11:38:48 AM   [InstanceLoader] AuthModule dependencies initialized +0ms
[Nest] 372   - 07/21/2020, 11:38:48 AM   [InstanceLoader] AppModule dependencies initialized +1ms
[Nest] 372   - 07/21/2020, 11:38:51 AM   [MongooseModule] Unable to connect to the database. Retrying (2)... +3005ms
[Nest] 372   - 07/21/2020, 11:38:54 AM   [MongooseModule] Unable to connect to the database. Retrying (3)... +3006ms
[Nest] 372   - 07/21/2020, 11:38:57 AM   [MongooseModule] Unable to connect to the database. Retrying (4)... +3007ms
[Nest] 372   - 07/21/2020, 11:39:00 AM   [MongooseModule] Unable to connect to the database. Retrying (5)... +3007ms
[Nest] 372   - 07/21/2020, 11:39:03 AM   [MongooseModule] Unable to connect to the database. Retrying (6)... +3006ms
[Nest] 372   - 07/21/2020, 11:39:06 AM   [MongooseModule] Unable to connect to the database. Retrying (7)... +3006ms
[Nest] 372   - 07/21/2020, 11:39:09 AM   [MongooseModule] Unable to connect to the database. Retrying (8)... +3003ms
[Nest] 372   - 07/21/2020, 11:39:12 AM   [MongooseModule] Unable to connect to the database. Retrying (9)... +3005ms
[Nest] 372   - 07/21/2020, 11:39:12 AM   [ExceptionHandler] mongoose_sequence_1.default is not a function +1ms
TypeError: mongoose_sequence_1.default is not a function
    at connectionFactory (/Users/tbruner/dev/s4n3/dist/app.module.js:31:70)
    at Function.<anonymous> (/Users/tbruner/dev/s4n3/node_modules/@nestjs/mongoose/dist/mongoose-core.module.js:59:28)
    at Generator.next (<anonymous>)
    at /Users/tbruner/dev/s4n3/node_modules/@nestjs/mongoose/dist/mongoose-core.module.js:20:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/tbruner/dev/s4n3/node_modules/@nestjs/mongoose/dist/mongoose-core.module.js:16:12)
    at /Users/tbruner/dev/s4n3/node_modules/@nestjs/mongoose/dist/mongoose-core.module.js:58:49
    at Observable._subscribe (/Users/tbruner/dev/s4n3/node_modules/rxjs/internal/observable/defer.js:10:21)
    at Observable._trySubscribe (/Users/tbruner/dev/s4n3/node_modules/rxjs/internal/Observable.js:44:25)
    at Observable.subscribe (/Users/tbruner/dev/s4n3/node_modules/rxjs/internal/Observable.js:30:22)
    at RetryWhenSubscriber.notifyNext (/Users/tbruner/dev/s4n3/node_modules/rxjs/internal/operators/retryWhen.js:85:21)
    at InnerSubscriber._next (/Users/tbruner/dev/s4n3/node_modules/rxjs/internal/InnerSubscriber.js:28:21)
    at InnerSubscriber.Subscriber.next (/Users/tbruner/dev/s4n3/node_modules/rxjs/internal/Subscriber.js:66:18)
    at Notification.observe (/Users/tbruner/dev/s4n3/node_modules/rxjs/internal/Notification.js:22:50)
    at AsyncAction.DelaySubscriber.dispatch (/Users/tbruner/dev/s4n3/node_modules/rxjs/internal/operators/delay.js:54:40)
    at AsyncAction._execute (/Users/tbruner/dev/s4n3/node_modules/rxjs/internal/scheduler/AsyncAction.js:71:18)

任何想法,为什么插件无法连接到 mongodb? mongodb 正在运行,我通过 mongo 客户端连接到它,当我删除 const AutoIncrement...connection.plugin(... 行时。

更新: 将 AutoIncrementFactory 的导入更改为: import AutoIncrementFactory = require('mongoose-sequence'); 产生超过最大呼叫统计大小:

更新 2:在下面进行了建议的更改,同样的错误。

[Nest] 68822   - 07/22/2020, 9:12:20 AM   [NestFactory] Starting Nest application...
[Nest] 68822   - 07/22/2020, 9:12:20 AM   [InstanceLoader] MongooseModule dependencies initialized +30ms
[Nest] 68822   - 07/22/2020, 9:12:20 AM   [InstanceLoader] AuthModule dependencies initialized +0ms
[Nest] 68822   - 07/22/2020, 9:12:20 AM   [InstanceLoader] AppModule dependencies initialized +0ms
[Nest] 68822   - 07/22/2020, 9:12:20 AM   [InstanceLoader] MongooseCoreModule dependencies initialized +11ms
[Nest] 68822   - 07/22/2020, 9:12:21 AM   [ExceptionHandler] Maximum call stack size exceeded +310ms
RangeError: Maximum call stack size exceeded
    at Object.clone (/Users/tbruner/dev/s4n3/node_modules/mongoose/lib/helpers/clone.js:53:16)
    at Schema.path (/Users/tbruner/dev/s4n3/node_modules/mongoose/lib/schema.js:678:24)
    at Schema.add (/Users/tbruner/dev/s4n3/node_modules/mongoose/lib/schema.js:519:14)
    at new Schema (/Users/tbruner/dev/s4n3/node_modules/mongoose/lib/schema.js:130:10)
    at Mongoose.Schema (/Users/tbruner/dev/s4n3/node_modules/mongoose/lib/schema.js:95:12)
    at SequenceFactory.Sequence._createCounterModel (/Users/tbruner/dev/s4n3/node_modules/mongoose-sequence/lib/sequence.js:170:36)
    at SequenceFactory.Sequence.enable (/Users/tbruner/dev/s4n3/node_modules/mongoose-sequence/lib/sequence.js:103:31)
    at SequenceFactory.Sequence.getInstance (/Users/tbruner/dev/s4n3/node_modules/mongoose-sequence/lib/sequence.js:92:14)
    at Schema.plugin (/Users/tbruner/dev/s4n3/node_modules/mongoose/lib/schema.js:1500:3)
    at applyPlugins (/Users/tbruner/dev/s4n3/node_modules/mongoose/lib/helpers/schema/applyPlugins.js:11:14)
    at NativeConnection.Connection.model (/Users/tbruner/dev/s4n3/node_modules/mongoose/lib/connection.js:1070:5)
    at SequenceFactory.Sequence._createCounterModel (/Users/tbruner/dev/s4n3/node_modules/mongoose-sequence/lib/sequence.js:191:23)
    at SequenceFactory.Sequence.enable (/Users/tbruner/dev/s4n3/node_modules/mongoose-sequence/lib/sequence.js:103:31)
    at SequenceFactory.Sequence.getInstance (/Users/tbruner/dev/s4n3/node_modules/mongoose-sequence/lib/sequence.js:92:14)
    at Schema.plugin (/Users/tbruner/dev/s4n3/node_modules/mongoose/lib/schema.js:1500:3)
    at applyPlugins (/Users/tbruner/dev/s4n3/node_modules/mongoose/lib/helpers/schema/applyPlugins.js:11:14)

【问题讨论】:

【参考方案1】:

mongoose-sequence 不是 ES6 模块。 在第一个版本中,您使用导入,而在第二个版本中,您将 importrequire 混合使用,这是不允许的。 你应该试试

const AutoIncrementFactory = require('mongoose-sequence');

【讨论】:

进行了建议的更改,我得到了与以前一样的最大调用堆栈大小超出错误。 我有同样的问题,我也尝试了这里提供的解决方案***.com/questions/62824276/… 但是当我生成一个新模型时,orderId 没有填充。

以上是关于NestJS 和 Mongoose 使用 Mongoose-Sequence的主要内容,如果未能解决你的问题,请参考以下文章

NestJS 和 Mongoose 使用 Mongoose-Sequence

如何在 NestJs 和 typescript 中使用 `mongoose-delete` 插件?

使用 nestjs 和 mongoose 哈希密码在生产中崩溃

nestjs : 定义要在 mongoose 和 graphql 中使用的地图/数组

使用 mongoose 和 nestjs 进行分页

如何使用 Jest 和 NestJS 模拟 Mongoose 的“lean()”查询?