【问题标题】:Argument of type '{ useNewUrlParser: boolean; useUnifiedTopology: boolean}' is not assignable to parameter of type''{ useNewUrlParser: boolean; 类型的参数useUnifiedTopology: boolean}' 不可分配给参数类型'
【发布时间】:2021-10-18 17:06:18
【问题描述】:

我正在尝试将我的 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'

有人可以帮我吗?非常感谢您对此的任何想法。

谢谢

【问题讨论】:

  • Mongoose 有自己的类型,@types/mongoose 已弃用:npmjs.com/package/@types/mongoose
  • @jonrsharpe 但是旧代码仍然可以正常工作吗?因为我根本没有更新到最新的。我很困惑为什么会出现这个问题
  • 我怀疑这是因为你以前有选项作为变量,现在有一个对象文字。不会检查变量是否有多余的属性,而对象文字会检查:playground
  • 但这工作正常。我从未更改过此代码。它总是如上所示。我可以想象如果我更新 typescript 包或我没有更新的 @types/mongoose 会发生这种情况
  • 这不是新的 api 驱动程序客户端选项的一部分。仅供参考

标签: typescript mongodb mongoose


【解决方案1】:

npm 包 "@types/mongoose" 已被弃用,因为最新版本的 Mongoose 发布了自己的类型。

https://www.npmjs.com/package/@types/mongoose

因此,最新版本的 mongoose (6.0.3) 不提供 useNewUrlParser、useCreateIndex、useUnifiedTopology 和 useFindAndModify 的连接选项。

要解决这个问题,请将 mongoose 降级到旧版本,如 5.13.8,然后它应该可以正常工作而没有任何问题。 :)

【讨论】:

    【解决方案2】:

    您可以将连接功能更改为

    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";
    

    【讨论】:

      【解决方案3】:

      将猫鼬更新到最新版本。删除类型/猫鼬解决了这个问题。

      【讨论】:

        猜你喜欢
        • 2021-11-23
        • 2019-10-26
        • 1970-01-01
        • 2017-06-29
        • 1970-01-01
        • 1970-01-01
        • 2018-11-25
        • 2017-02-12
        • 1970-01-01
        相关资源
        最近更新 更多