类型参数 '{ useNewUrlParser: boolean; useUnifiedTopology: boolean}' 不可分配给类型' 的参数

2023-12-27

我正在尝试将 Node.js 服务器连接到 Atlas mongo 数据库。我为此使用猫鼬。

await mongoose
      .connect(process.env.MONGO_URI!, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        useCreateIndex: true,
        useFindAndModify: false,
        poolSize: parseInt(process.env.POOL_SIZE!),
      })
      .then((res) => {
        console.log(
          'Connected to Distribution API Database - Initial Connection'
        );
      })
      .catch((err) => {
        console.log(
          `Initial Distribution API Database connection error occured -`,
          err
        );
      });

我在 package.json 文件中与此相关的依赖项如下

"dependencies": {
    "@types/mongoose": "5.7.29",
    "mongoose": "5.9.21",
    "typescript": "3.9.5"
  },

这是早些时候工作的,没有任何问题(我根本没有更新 @types/mongoose 或 mongoose 版本),现在突然出现以下错误

Compilation error in /app/src/index.ts
Using ts-node version 8.10.2, typescript version 3.9.5
[ERROR] 16:25:18 ⨯ Unable to compile TypeScript: src/index.ts(59,11): error TS2769: No overload matches this call.
  Overload 1 of 3, '(uris: string, callback: (err: MongoError) => void): Promise<typeof import("mongoose")>', gave the following error.
    Argument of type '{ useNewUrlParser: boolean; useUnifiedTopology: boolean; useCreateIndex: boolean; useFindAndModify: boolean; poolSize: number; }' is not assignable to parameter of type '(err: MongoError) => void'.
      Object literal may only specify known properties, and 'useNewUrlParser' does not exist in type '(err: MongoError) => void'.
  Overload 2 of 3, '(uris: string, options?: ConnectionOptions | undefined): Promise<typeof import("mongoose")>', gave the following error.
    Argument of type '{ useNewUrlParser: true; useUnifiedTopology: true; useCreateIndex: true; useFindAndModify: false; poolSize: number; }' is not assignable to parameter of type 'ConnectionOptions'.
      Object literal may only specify known properties, and 'poolSize' does not exist in type 'ConnectionOptions'

有人可以帮我解决这个问题吗?非常感谢对此的任何想法。

Thanks


您可以将连接功能更改为

await mongoose
      .connect(process.env.MONGO_URI!, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        useCreateIndex: true,
        useFindAndModify: false,
        poolSize: parseInt(process.env.POOL_SIZE!),
      } as ConnectOptions)
      .then((res) => {
        console.log(
          'Connected to Distribution API Database - Initial Connection'
        );
      })
      .catch((err) => {
        console.log(
          `Initial Distribution API Database connection error occured -`,
          err
        );
      });

您可以使用它来导入 ConnectOptions

import mongoose, { ConnectOptions } from "mongoose";
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

类型参数 '{ useNewUrlParser: boolean; useUnifiedTopology: boolean}' 不可分配给类型' 的参数 的相关文章

随机推荐