使用Nestjs/Angular初始化MongoDB时出错

在付出了很多努力但没有成功之后,我终于来到了这里。我学习 Nestjs/Angular/MongoDB。到目前为止,我成功地同时运行了 Angular 服务器和 Nestjs 服务器。但是当我用它们初始化 mongoDB 时,我得到了大量的错误 147(主要与模式相关)。

错误似乎与我的代码无关,而是安装依赖项。无论如何,我也会在我的代码下复制。我在 ubuntu 和 windows 上都尝试了这个应用程序。但同样的错误仍然存​​在。

app.controller.ts:

import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
 export class AppController {
 constructor(private readonly appService: AppService) {}

@Get()
 healthCheck(): string {
  return this.appService.appStatus();
  }
}

应用模块.ts

import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { config } from './config';

@Module({
  imports: [
    MongooseModule.forRoot(config.mongoUri, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useCreateIndex: true,
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

应用服务.ts

import { Injectable } from '@nestjs/common';
import { InjectConnection } from '@nestjs/mongoose';
import { Connection } from 'mongoose';
import { config } from './config';

@Injectable()
export class AppService {
  constructor(@InjectConnection() private connection: Connection) {}
   appStatus(): string {
   return `${config.appName} is running in port ${config.port}. Connected to 
    ${this.connection.name}`;
  }
}

配置文件

import * as dotenv from 'dotenv';

const result = dotenv.config();

 if (result?.error) {
 throw new Error('Add .env file');
}

export const config = {
  env: process.env.SZ_ENV,
  appName: process.env.SZ_APP,
  port: process.env.SZ_PORT,
  mongoUri:  
 `mongodb+srv://${process.env.SZ_MONGO_USER}:${process.env.SZ_MONGO_PASS}@
  ${process.env.SZ_MONGO_HOST}/${process.env.SZ_MONGO_DB} 
  authSource=admin&replicaSet=${process.env.SZ_MONGO_REPLICA}&
  readPreference=primary&ssl=true`,
  };

主文件

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { config } from './config';

 async function bootstrap() {
  const { appName, port } = config;
  const app = await NestFactory.create(AppModule);
  await app.listen(port, () => {
    console.info(`${appName} is running in http://localhost:${port}`);
  });
 }
bootstrap();

错误:而纱线启动:服务器

[8:16:58 PM] Starting compilation in watch mode...

node_modules/@nestjs/mongoose/dist/factories/schema.factory.d.ts:4:60 - error TS2315: Type 
'Schema' is not generic.

4     static createForClass<T = any>(target: Type<unknown>): mongoose.Schema<T>;
                                                         ~~~~~~~~~~~~~~~~~~
node_modules/@types/mongoose/index.d.ts:79:1 - error TS6200: Definitions of the following 
identifiers conflict with those in another file: DocumentDefinition, FilterQuery, 
UpdateQuery, NativeError, Mongoose, CastError, Collection, Connection, Error, QueryCursor, 
VirtualType, Schema, Subdocument, Array, DocumentArray, Buffer, ObjectId, Decimal128, Map, 
Aggregate, SchemaType, Document

79     declare module "mongoose" {

 node_modules/mongoose/index.d.ts:1:1
 1 declare module "mongoose" {
   ~~~~~~~
 Conflicts are in this file.

 node_modules/@types/mongoose/index.d.ts:226:14 - error TS2403: Subsequent variable 
 declarations must have the same type.  Variable 'SchemaTypes' must be of type 'typeof 
 Types', but here has type 'typeof Types'.

 226   export var SchemaTypes: typeof Schema.Types;
              ~~~~~~~~~~~

 node_modules/mongoose/index.d.ts:45:14
 45   export var SchemaTypes: typeof Schema.Types;
                 ~~~~~~~~~~~
 'SchemaTypes' was also declared here.

 node_modules/@types/mongoose/index.d.ts:822:24 - error TS2314: Generic type 
 'Query<ResultType, DocType, T>' requires 3 type argument(s).

 822     constructor(query: Query<T>, options: any);
                        ~~~~~~~~

 node_modules/@types/mongoose/index.d.ts:1013:19 - error TS2314: Generic type 
 'Query<ResultType, DocType, T>' requires 3 type argument(s).

 1013     pre<T extends Query<any> = Query<any>>(
                    ~~~~~~~~~~

 node_modules/@types/mongoose/index.d.ts:1013:32 - error TS2314: Generic type 
 'Query<ResultType, DocType, T>' requires 3 type argument(s).

 1013     pre<T extends Query<any> = Query<any>>(
                                 ~~~~~~~~~~

 node_modules/@types/mongoose/index.d.ts:1036:48 - error TS2314: Generic type 
 'Query<ResultType, DocType, T>' requires 3 type argument(s).

 1036     pre<T extends Document | Model<Document> | Query<any> | Aggregate<any>>(
                                                 ~~~~~~~~~~

 node_modules/@types/mongoose/index.d.ts:1048:19 - error TS2314: Generic type 
 'Query<ResultType, DocType, T>' requires 3 type argument(s).

 1048     pre<T extends Query<any> = Query<any>>(
                    ~~~~~~~~~~

 node_modules/@types/mongoose/index.d.ts:1048:32 - error TS2314: Generic type 
 'Query<ResultType, DocType, T>' requires 3 type argument(s).

 1048     pre<T extends Query<any> = Query<any>>(
                                 ~~~~~~~~~~

 node_modules/@types/mongoose/index.d.ts:1074:48 - error TS2314: Generic type 
 'Query<ResultType, DocType, T>' requires 3 type argument(s).

 1074     pre<T extends Document | Model<Document> | Query<any> | Aggregate<any>>(
                                                 ~~~~~~~~~~

 node_modules/@types/mongoose/index.d.ts:1264:5 - error TS2374: Duplicate string index 
 signature.

 1264     [path: string]: SchemaTypeOpts<any> | Schema | SchemaType;
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 node_modules/@types/mongoose/index.d.ts:1627:76 - error TS2314: Generic type 
'Query<ResultType, 
   DocType, T>' requires 3 type argument(s).

 1627     replaceOne(replacement: any, callback?: (err: any, raw: any) => void): Query<any>;

回答

新的猫鼬v5.11.x发布了自己的类型定义,所以你不应该使用@类型/猫鼬
https://github.com/Automattic/mongoose/issues/9606#issuecomment-736710621


回答

这很可能与Mongoose v5.11.0相关,它添加了自己的类型。恢复到 v5.10.x 应该可以修复它

  • To prevent updating Mongoose to `> 5.11.x` you can change in package.json` `"mongoose": "^5.10.x"` to `"mongoose": "~5.10.x"`.

以上是使用Nestjs/Angular初始化MongoDB时出错的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>