From 4be889ea63838c53f06a6a061733de94cc62f51e Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Wed, 12 Jun 2019 13:33:31 +0800 Subject: [PATCH] fix: db init error --- packages/cc-server-db-mongo/src/client/index.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/cc-server-db-mongo/src/client/index.ts b/packages/cc-server-db-mongo/src/client/index.ts index 6b28de4..e1887c6 100644 --- a/packages/cc-server-db-mongo/src/client/index.ts +++ b/packages/cc-server-db-mongo/src/client/index.ts @@ -5,12 +5,16 @@ import { DBClient } from 'cc-server-db' @provide(DBClient) export class MongoDBClient implements DBClient { + private table: string; private db: Db; private collection: Collection; constructor() { MongoDBConnection.getConnection((connection) => { this.db = connection; + if (this.table) { + this.collection = this.db.collection(this.table); + } }); } @@ -19,7 +23,10 @@ export class MongoDBClient implements DBClient { } 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 {