'Getting no overload matches this call running mongoose with typescript and express

I am getting the below error for useNewUrlParser, useUnifiedTopology, useCreateIndex:

No overload matches this call.
  Overload 1 of 3, '(uri: string, callback: CallbackWithoutResult): void', gave the following error.
    Argument of type '{ useNewUrlParser: boolean; useUnifiedTopology: boolean; useCreateIndex: boolean; }' is not assignable to parameter of type 'CallbackWithoutResult'.
      Object literal may only specify known properties, and 'useNewUrlParser' does not exist in type 'CallbackWithoutResult'.
  Overload 2 of 3, '(uri: string, options?: ConnectOptions | undefined): Promise<typeof import("mongoose")>', gave the following error.
    Argument of type '{ useNewUrlParser: boolean; useUnifiedTopology: boolean; useCreateIndex: boolean; }' is not assignable to parameter of type 'ConnectOptions'.
      Object literal may only specify known properties, and 'useNewUrlParser' does not exist in type 'ConnectOptions'.

Index.ts

import express from 'express'
import { errorHandler } from './middleware/error-handler';
import router from './router'
import mongoose from 'mongoose'

const app = express()

app.use(express.json())
app.use('/api/users', router)
app.use(errorHandler)

const start = async () => {
  try {
    await mongoose.connect("mongodb://auth-mongo-srv:27017/auth", {
      useNewUrlParser: true,    // error here
      useUnifiedTopology: true,
      useCreateIndex: true,
    });
    console.log("Connected to MongoDb");
  } catch (err) {
    console.error(err);
  }
};

const PORT = 3000
app.listen(PORT, () => {
    console.log("working on port ",PORT)
})

start()

tsconfig.json

{
  "compilerOptions": {
"target": "es6",
"module": "commonjs", 
"esModuleInterop": true, 
"forceConsistentCasingInFileNames": true,
"strict": true, 
"skipLibCheck": true
 }
}

I have mongoose 6.02 installed in package.json and if it is of any help, I am getting mongo from dockerhub and running in a cluster pod.

Any help or suggestion is appreciated.



Solution 1:[1]

ConnectOptions of mongoose for version 6.0.2 does not include useNewUrlParser, useUnifiedTopology, useCreateIndex. However, mongoose docs for 6.0.2 still shows those options to be valid and without those options, mongoose is throwing a topology error.

Downgraded mongoose to version 5.13.8 and now it is working without problems.

Solution 2:[2]

Just remove second arg from mongoose.connect()

await mongoose.connect("mongodb://auth-mongo-srv:27017/auth")

Solution 3:[3]

Just remove those options, they are no longer needed

https://mongoosejs.com/docs/migrating_to_6.html#the-aspromise-method-for-connections

useNewUrlParser, useUnifiedTopology, useFindAndModify, and useCreateIndex are no longer supported options. Mongoose 6 always behaves as if useNewUrlParser, useUnifiedTopology, and useCreateIndex are true, and useFindAndModify is false. Please remove these options from your code.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shiladitya Thakur
Solution 2 Konstantin
Solution 3 Alex G