1
0
Fork 0

fix: db init error

master
MiaoWoo 2019-06-12 13:33:31 +08:00
parent 86cf26355e
commit 4be889ea63
1 changed files with 8 additions and 1 deletions

View File

@ -5,12 +5,16 @@ import { DBClient } from 'cc-server-db'
@provide(DBClient) @provide(DBClient)
export class MongoDBClient<T = any> implements DBClient { export class MongoDBClient<T = any> implements DBClient {
private table: string;
private db: Db; private db: Db;
private collection: Collection; private collection: Collection;
constructor() { constructor() {
MongoDBConnection.getConnection((connection) => { MongoDBConnection.getConnection((connection) => {
this.db = connection; this.db = connection;
if (this.table) {
this.collection = this.db.collection(this.table);
}
}); });
} }
@ -19,7 +23,10 @@ export class MongoDBClient<T = any> implements DBClient {
} }
public setTable(table: string): void { public setTable(table: string): void {
this.collection = this.db.collection(table); this.table = table;
if (this.db) {
this.collection = this.db.collection(table);
}
} }
public async find(filter: object): Promise<T[]> { public async find(filter: object): Promise<T[]> {