1

The class below throws the error

Type 'typeof import("mongoose")' is missing the following properties from type 'Db': serverConfig, bufferMaxEntries, databaseName, options, and 37 more.

I cannot find out what the return type of mongoose.connect is.

import mongoose from "mongoose";
import {Db} from "mongodb";

interface MongoDbConfig {
   server: String, 
   port: String,
   dbName: String;
} 
// TODO: make singelton
class MongoDb {
    private db : Db;
    private _server : String;
    private _port : String;
    private _dbName : String;

    constructor(config: MongoDbConfig){
            this._server = config.server;
            this._port = config.port;
            this._dbName = config.dbName

    }

    public async connect() {
        const uri = "mongodb://"+this._server+":"+this._port+"/"+this._dbName;
        this.db = await mongoose.connect(uri, { useNewUrlParser: true }); // error
        console.log(typeof this.db)
        console.log("Connected to db");
        return this.db;
    }

    public getDb(){
        return this.db;
    }
}

3 Answers 3

2

The issue seems comes from typing declaration in the code for db variable. The type definition mentioned that the connect function returns Promise<Mongoose> but this.db has Db type instead of Mongoose.

This may solve the issue

private db: mongoose.Mongoose; // change from Db to mongoose.Mongoose

// ...

this.db = await mongoose.connect(uri, { useNewUrlParser: true }); 

Hope it helps

Sign up to request clarification or add additional context in comments.

Comments

0

I checked the doc: here is what it is saying:

The connect() function also accepts a callback parameter and returns a promise.

mongoose.connect(uri, options, function(error) {
  // Check error in initial connection. There is no 2nd param to the callback.
});

// Or using promises
mongoose.connect(uri, options).then(
  () => { /** ready to use. The `mongoose.connect()` promise resolves to mongoose instance. */ },
  err => { /** handle initial connection error */ }
);

As you see, it actually return a promise, and this promise resolves to mongoose instance

I checked the mongoose's index.d.ts as well, what I find is:

/**
 * Opens the default mongoose connection.
 * Options passed take precedence over options included in connection strings.
 * @returns pseudo-promise wrapper around this
 */
export function connect(uris: string, options: ConnectionOptions, callback: (err: mongodb.MongoError) => void): Promise<Mongoose>;
export function connect(uris: string, callback: (err: mongodb.MongoError) => void): Promise<Mongoose>;
export function connect(uris: string, options?: ConnectionOptions): Promise<Mongoose>;

So it looks it it returning Promise<Mongoose> and since you are using async/await, the resolve value will just be Mongoose.

Bye the way, not sure what you are doing here, but i would recommend just follow as the doc says when connecting to database:

mongoose.connect('mongodb://localhost/test', { useNewUrlParser: true });
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  console.log('Successfully connected to db')
});

Your this.db probably should equal to mongoose.connection which having a type of mongoose.Connection, rather than the resolve result of mongoose.connect(...)?

Hope this helps.

Comments

0

You can use it as below.

import { connect, Mongoose } from 'mongoose';
//...
private db: Mongoose;
//...
this.db = await connect(uri, { useNewUrlParser: true });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.