Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 799
Shanka Somasiri Avatar asked Dec 09 '22 23:12

Shanka Somasiri


2 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";
like image 73
rg-rupeee Avatar answered May 29 '23 05:05

rg-rupeee


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. :)

like image 22
Bryan Kee Avatar answered May 29 '23 07:05

Bryan Kee