My GitHub repo: https://github.com/safiullah7/legan
Branch: redux
I'm following this tutorial: https://tomanagle.medium.com/build-a-rest-api-with-node-js-typescript-mongodb-b6c898d70d61 and I'm unable to connect to my mongodb. Here's my code file where I'm trying to connect with the mongodb:
import config from "config";
import log from "../logger";
function connect() {
const dbUri = config.get("dbUri") as string;
return mongoose
.connect(dbUri, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
log.info("Database connected");
})
.catch((error) => {
log.error("db error", error);
process.exit(1);
});
}
export default connect;
Compiler gives the following error:
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; }' 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; }' is not assignable to parameter of type 'ConnectOptions'.
Object literal may only specify known properties, and 'useNewUrlParser' does not exist in type 'ConnectOptions'.
I'm new to typescript and node/mongoos. Would appreciate your help.
The new version of mongoose
(the latest version when I wrote this post is 6.0.2
) has the following type definitions for the connect()
function.
/** 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>;
The options object
you're passing to the connect()
function
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
is, therefore, should have a type of ConnectOptions
.
Current definition of the ConnectOptions
is as follows:
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;
}
Looking at the new definition of the connect
and ConnectOptions
, we can see that there is no definition of useNewUrlParser
or useUnifiedTopology
inside the ConnectOptions
. That is the reason we got such an error. You can delete the options useNewUrlParser: true,
and useUnifiedTopology: true,
and your code should be able to connect to your MongoDB. If you want to pass in some options to the connect()
function, then they should follow the new type definition of the ConnectOptions
.