Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Options passed in mongoose.connect() function cause error #10632

Closed
TranXuanHoang opened this issue Aug 28, 2021 · 10 comments
Closed

Options passed in mongoose.connect() function cause error #10632

TranXuanHoang opened this issue Aug 28, 2021 · 10 comments
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Milestone

Comments

@TranXuanHoang
Copy link

Do you want to request a feature or report a bug?
bug

What is the current behavior?
An error occurs when trying to connect to a MongoDB

Connection Code

  try {
    await mongoose.connect(process.env.MONGO_URI, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useFindAndModify: false,
      useCreateIndex: true,
    })
    console.log('Connected to MongoDB')
  } catch (err) {
    console.error(err)
  }

Error Message

[ERROR] 05:31:02 ⨯ Unable to compile TypeScript:
src/index.ts(18,7): error TS2769: 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; useFindAndModify: 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; useFindAndModify: 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'.

If the current behavior is a bug, please provide the steps to reproduce.

  • Install mongoose version 6.0.2
    "mongoose": "^6.0.2"
  • Connect to the MongoDB using mongoose.connect()
      try {
        await mongoose.connect(process.env.MONGO_URI, {
          useNewUrlParser: true,
          useUnifiedTopology: true,
          useFindAndModify: false,
          useCreateIndex: true,
        })
        console.log('Connected to MongoDB')
      } catch (err) {
        console.error(err)
      }
  • TypeScript config tsconfig.json
    {
      "compilerOptions": {
        /* Visit https://aka.ms/tsconfig.json to read more about this file */
    
        /* Basic Options */
        "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
        "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
        "declaration": true,                      /* Generates corresponding '.d.ts' file. */
        "outDir": "./build",                      /* Redirect output structure to the directory. */
    
        /* Strict Type-Checking Options */
        "strict": true,                           /* Enable all strict type-checking options. */
    
        /* Module Resolution Options */
        "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
      
        /* Advanced Options */
        "skipLibCheck": true,                     /* Skip type checking of declaration files. */
        "forceConsistentCasingInFileNames": true  /* Disallow inconsistently-cased references to the same file. */
      }
    }

What is the expected behavior?
Should be able to pass in an options object to the connect() function with the following options.

    {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useFindAndModify: false,
      useCreateIndex: true,
    }

What are the versions of Node.js, Mongoose, and MongoDB you are using? Note that "latest" is not a version.
Node.js: v16.7.0
Mongoose: 6.0.2
MongoDB: 4.4.8

@lorand-horvath
Copy link
Contributor

This is a breaking change in Mongoose 6, those 4 options should be removed from mongoose.connect() in version 6.
https://mongoosejs.com/docs/migrating_to_6.html#no-more-deprecation-warning-options

@jordankkk
Copy link

other options except useNewUrlParser, useUnifiedTopology, useFindAndModify, and useCreateIndex still cause the error

@lorand-horvath
Copy link
Contributor

other options except useNewUrlParser, useUnifiedTopology, useFindAndModify, and useCreateIndex still cause the error

which ones?

@moisesbites
Copy link

moisesbites commented Aug 28, 2021

other options except useNewUrlParser, useUnifiedTopology, useFindAndModify, and useCreateIndex still cause the error

which ones?

useCreateIndex and useFindAndModify -> MongoParseError option ... is not supported

Maybe it causes this Issue #10631 ?

@lorand-horvath
Copy link
Contributor

Again, according to https://mongoosejs.com/docs/migrating_to_6.html#no-more-deprecation-warning-options
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.
Simply remove all 4 options in Mongoose 6.

@TranXuanHoang
Copy link
Author

@lorand-horvath Thanks for your clarification!
@moisesbites I just checked the type definition of the connect() function and its options parameter. Here is a list of options we can pass into the connect function (no definition of useNewUrlParser, useUnifiedTopology, useFindAndModify, and useCreateIndex):

  /** Opens Mongoose's default connection to MongoDB, see [connections docs](https://mongoosejs.com/docs/connections.html) */
  export function connect(uri: string, options: ConnectOptions, callback: CallbackWithoutResult): void;
  export function connect(uri: string, callback: CallbackWithoutResult): void;
  export function connect(uri: string, options?: ConnectOptions): Promise<Mongoose>;
  interface ConnectOptions extends mongodb.MongoClientOptions {
    /** Set to false to [disable buffering](http://mongoosejs.com/docs/faq.html#callback_never_executes) on all models associated with this connection. */
    bufferCommands?: boolean;
    /** The name of the database you want to use. If not provided, Mongoose uses the database name from connection string. */
    dbName?: string;
    /** username for authentication, equivalent to `options.auth.user`. Maintained for backwards compatibility. */
    user?: string;
    /** password for authentication, equivalent to `options.auth.password`. Maintained for backwards compatibility. */
    pass?: string;
    /** Set to false to disable automatic index creation for all models associated with this connection. */
    autoIndex?: boolean;
    /** Set to `true` to make Mongoose automatically call `createCollection()` on every model created on this connection. */
    autoCreate?: boolean;
  }

@AhmedBHameed
Copy link

I managed to run with the following configuration:

  const connectionResult = await callTryCatch<Mongoose, Error>(() =>
    connect(`mongodb://${DB_SERVER}:${DB_PORT}`, {
      dbName: DB_NAME,
      auth: {
        password: DB_PASS,
        username: DB_USER_NAME,
      },
    })
  );

I'm using v6.0.2

Good luck.

@Manas-Nagelia
Copy link

Again, according to https://mongoosejs.com/docs/migrating_to_6.html#no-more-deprecation-warning-options
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.
Simply remove all 4 options in Mongoose 6.

Thanks. I was following around with a MERN stack tutorial, using Mongoose v6.0.2, and I was getting some weird errors. I tried commenting useCreateIndex, and it worked. Since Mongose 6 by default has these options as true, I removed all of my options, and it still works. Many thanks to you.

@theonlydaleking
Copy link
Contributor

Possible opportunity to update typescript docs here

@IslandRhythms IslandRhythms added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Aug 30, 2021
@vkarpov15 vkarpov15 modified the milestones: 6.0.3, 6.0.4 Aug 30, 2021
theonlydaleking added a commit to theonlydaleking/mongoose that referenced this issue Aug 30, 2021
@fsdramjan
Copy link

Do you want to request a feature or report a bug? bug

What is the current behavior? An error occurs when trying to connect to a MongoDB

Connection Code

  try {
    await mongoose.connect(process.env.MONGO_URI, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useFindAndModify: false,
      useCreateIndex: true,
    })
    console.log('Connected to MongoDB')
  } catch (err) {
    console.error(err)
  }

Error Message

[ERROR] 05:31:02 ⨯ Unable to compile TypeScript:
src/index.ts(18,7): error TS2769: 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; useFindAndModify: 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; useFindAndModify: 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'.

If the current behavior is a bug, please provide the steps to reproduce.

  • Install mongoose version 6.0.2
    "mongoose": "^6.0.2"

  • Connect to the MongoDB using mongoose.connect()

      try {
        await mongoose.connect(process.env.MONGO_URI, {
          useNewUrlParser: true,
          useUnifiedTopology: true,
          useFindAndModify: false,
          useCreateIndex: true,
        })
        console.log('Connected to MongoDB')
      } catch (err) {
        console.error(err)
      }
  • TypeScript config tsconfig.json

    {
      "compilerOptions": {
        /* Visit https://aka.ms/tsconfig.json to read more about this file */
    
        /* Basic Options */
        "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
        "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
        "declaration": true,                      /* Generates corresponding '.d.ts' file. */
        "outDir": "./build",                      /* Redirect output structure to the directory. */
    
        /* Strict Type-Checking Options */
        "strict": true,                           /* Enable all strict type-checking options. */
    
        /* Module Resolution Options */
        "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
      
        /* Advanced Options */
        "skipLibCheck": true,                     /* Skip type checking of declaration files. */
        "forceConsistentCasingInFileNames": true  /* Disallow inconsistently-cased references to the same file. */
      }
    }

What is the expected behavior? Should be able to pass in an options object to the connect() function with the following options.

    {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useFindAndModify: false,
      useCreateIndex: true,
    }

What are the versions of Node.js, Mongoose, and MongoDB you are using? Note that "latest" is not a version. Node.js: v16.7.0 Mongoose: 6.0.2 MongoDB: 4.4.8

Hey, I've solve this with this import in index.ts file - const mongoose = require("mongoose");

@Automattic Automattic locked and limited conversation to collaborators Feb 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

10 participants