Argument of type '{ useNewUrlParser: boolean; useUnifiedTopology: boolean}' is not assignable to parameter of type'

Viewed 6888

I am trying to connect my Node.js server to Atlas mongo database. I am using mongoose for this.

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
        );
      });

My dependencies related to this in the package.json file is as below

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

This was working earlier without any issues (I did not update @types/mongoose or mongoose versions at all) and suddenly now I am getting the below error

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'

Can someone help me on this?? Really appreciate any thoughts on this.

Thanks

4 Answers

You can change your connect function to

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
        );
      });

You can use this to import ConnectOptions

import mongoose, { ConnectOptions } from "mongoose";

npm package "@types/mongoose" has been deprecated as the latest version of Mongoose publishes its own types.

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

Therefore, the latest version of mongoose (6.0.3) doesn't provide connect options for useNewUrlParser, useCreateIndex, useUnifiedTopology, and useFindAndModify.

To solve this, downgrade mongoose to older version like 5.13.8, then it should be working fine without any issues. :)

Updated the mongoose to the latest. Removed type/mongoose solved the issue.

Mongoose Updated so, you need to change useCreateIndex:true to autoIndex:true only and will work:

import mongoose, {ConnectOptions} from "mongoose";

mongoose
  .connect(process.env.URLDBMONGO || "http://localhost:8000", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    autoIndex: true,
  } as ConnectOptions)
  .then((db) => {
    console.log("Database Connected Successfuly.");
  })
  .catch((err) => {
    console.log("Error Connectiong to the Database");
  });
Related