Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.92k stars 3.84k forks source link

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

Closed TranXuanHoang closed 3 years ago

TranXuanHoang commented 3 years ago

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.

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 commented 3 years ago

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 commented 3 years ago

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

lorand-horvath commented 3 years ago

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

which ones?

moisesbites commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

Possible opportunity to update typescript docs here

fsdramjan commented 1 year ago

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