sfarzoso
sfarzoso

Reputation: 1610

Cannot return Promise

I'm using Typeorm so I've created a method that return a connection:

public static async getConnection(): Promise<Connection> {
    if (DatabaseProvider.connection) {
        return DatabaseProvider.connection;
    }

    const { type, host, port, username, password, database, extra, entities, migrations } = DatabaseProvider.configuration;
    DatabaseProvider.connection = await createConnection({
        type, host, port, username, password, database,
        extra,
        entities: [
            entities
        ],
        migrations: [
            migrations
        ]
    } as any); 

    return DatabaseProvider.connection;
}

I want assign the connection to a bot instance of Telegraf so I have created a .d.ts file for specify the type:

export interface TelegrafContext extends Context {
    db: Connection
}

then:

bot.context.db = DatabaseProvider.getConnection().then((conn) => { return conn; });

and I get:

Type 'Promise' is missing the following properties from type 'Connection': name, options, isConnected, driver, and 32 more.

What I did wrong?

Upvotes: 0

Views: 278

Answers (1)

Teneff
Teneff

Reputation: 32158

Probably because you're trying to assign Promise<Connection> to bot.context.db and it should be a Connection.

so you can either:

DatabaseProvider.getConnection().then((conn) => { 
  bot.context.db = conn
});

or:

bot.context.db = await DatabaseProvider.getConnection()

Upvotes: 3

Related Questions