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'.

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

Thanks

Hello @Shanka_Somasiri
Could you please copy and paste you index.ts here? I just do the same code, config, and dependencies but it’s kinda working fine.

btw, how do you execute your index.ts?

Hi @HomAskIe

import mongoose from 'mongoose';
import { app } from './app';
import http from 'http';

import { User, Agency } from '@ssomlk-org/distribution-common';
import { natsWrapper } from './nats-wrapper';

const validateEnvVariables = () => {
  if (!process.env.JWT_ACCESS_TOKEN_KEY) {
    throw new Error('JWT_ACCESS_TOKEN_KEY must be defined');
  }

  if (!process.env.JWT_REFRESH_TOKEN_KEY) {
    throw new Error('JWT_REFRESH_TOKEN_KEY must be defined');
  }

  if (!process.env.JWT_ACCESS_TOKEN_EXPIRES_IN) {
    throw new Error('JWT_ACCESS_TOKEN_EXPIRES_IN must be defined');
  }

  if (!process.env.JWT_REFRESH_TOKEN_EXPIRES_IN) {
    throw new Error('JWT_REFRESH_TOKEN_EXPIRES_IN must be defined');
  }

  if (!process.env.MONGO_URI) {
    throw new Error('MONGO_URI must be defined');
  }

  if (!process.env.NATS_CLIENT_ID) {
    throw new Error('NATS_CLIENT_ID must be defined');
  }

  if (!process.env.NATS_URL) {
    throw new Error('NATS_URL must be defined');
  }

  if (!process.env.NATS_CLUSTER_ID) {
    throw new Error('NATS_CLUSTER_ID must be defined');
  }

  if (!process.env.MAIL_USER) {
    throw new Error('MAIL_USER must be defined');
  }

};

const start = async () => {
  validateEnvVariables();

  try {
    await natsWrapper.connect(
      process.env.NATS_CLUSTER_ID!,
      process.env.NATS_CLIENT_ID!,
      process.env.NATS_URL!
    );

    natsWrapper.client.on('close', () => {
      console.log('NATS: backend-services connection closed!');
      process.exit();
    });

    process.on('SIGINT', () => natsWrapper.client.close());
    process.on('SIGTERM', () => natsWrapper.client.close());

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

    mongoose.connection.on('error', (err) => {
      console.log(`Distribution API Database connection error - ${err}`);
      process.exit();
    });

    let agency = await Agency.findOne({ code: 'DEVLABS' });

    if (!agency) {
      agency = Agency.build({
        code: 'DEVLABS',
        description: 'Software Company',
      });
      agency = await agency.save();
    }

    let admin = await User.findOne({ email: process.env.MAIL_USER });

    if (!admin) {
      admin = User.build({
        agency: agency,
        firstName: 'Test',
        lastName: 'Test',
        email: process.env.MAIL_USER!,
        role: 'developer',
        phone: '3647364746734',
        password: process.env.MAIL_PWD!,
        passwordConfirm: process.env.MAIL_PWD!,

      });

      admin.set('role', 'developer');
      await admin.save();
    }
  } catch (err) {
    console.log(err);
  }

  const serverHttp = http.createServer(app);
  let server = serverHttp.listen(3000, () => {
    console.log('Distribution API service listening on port 3000 :v1.0.0');
  });

  process.on('SIGINT', () => {
    server.close(() => {
      console.log(
        'Distribution API service shutting down due to signal Interruption - SIGINT'
      );
    });
  });

  process.on('SIGTERM', () => {
    server.close(() => {
      console.log(
        'Distribution API service shutting down due to signal termination - SIGTERM'
      );
    });
  });
};

start();

I execute my index.ts using below in package.json

"scripts": {
    "start": "ts-node-dev --poll src/index.ts"
  },

Thanks

Thank you for the information.

Please try to declare a option variable and pass to the connect method like below

  const options = {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useCreateIndex: true,
      useFindAndModify: false,
      poolSize: parseInt(process.env.POOL_SIZE!),
  };
  mongoose .connect(process.env.MONGO_URI!, options)
      .then((res) => {
        console.log(
          'Connected to Distribution API Database - Initial Connection'
        );
      })
      .catch((err) => {
        console.log(
          `Initial Distribution API Database connection error occured -`,
          err
        );
      });

Try to see if it works for you?

btw you should choose one of the way to open a new connection like in this example
https://mongoosejs.com/docs/connections.html#error-handling

I think you’re mixing asyn-await with promiss-callback

Looks like Mongooes 5.9.21. poolSize is not listed in ConnectionOption interface. So if you want to set value for it you may change it to

mongoose .connect(process.env.MONGO_URI!, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useCreateIndex: true,
    useFindAndModify: false,
    server: {
        poolSize: Number(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
    );
});

Let me know if that works to you

2 Likes

This worked perfectly. Thank alot for your help

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.