简体   繁体   English

如何查看Firebase数据库的连接或断开?

[英]How to view connection or disconnection to Firebase database?

I have a firebase database, I finally managed to build a class with set, get, delete properties included.我有一个 firebase 数据库,我终于成功地构建了一个 class,其中包含set, get, delete属性。

But I was wondering how to include the text that the database was connected or not connected using console.log in the node.js work environment of bot applications such as discord.js但是我想知道如何在discord.js等bot应用程序的discord.js工作环境中使用console.log包含数据库已连接或未连接的文本

class fireBase {
    constructor(client) {
        try {
            console.info(cyan('Firebase: database is now connected...'))
            if(!firebaseConfig || !firebaseConfig.credential || !firebaseConfig.databaseURL){
               throw ('config data must be added to connect to the database\n [credential], [databaseURL] in config.json')
            }
            this.client = client;
            this.app  = initializeApp({ credential: cert(firebaseConfig.credential), databaseURL: firebaseConfig.databaseURL });
            this.db  = getFirestore(this.app);

            console.info(green('Firebase: database connected successfully'))
        } catch (err) {
            throw new Error(red(err)).message
        }
    }

Find out the right way in node.js在node.js中找到正确的方法

First, I'd recommend not putting your setup code inside the constructor.首先,我建议不要将设置代码放在构造函数中。 The constructor should be used to setup the class instance and not have any side effects.构造函数应该用于设置 class 实例并且没有任何副作用。

Place most of your current procedure inside a login function, or whatever you prefer to call it.将您当前的大部分过程放在登录名 function 中,或者您喜欢的任何名称。

If you want to setup events for your class, have the class extend node's EventEmitter :如果你想为你的 class 设置事件,让 class 扩展节点的EventEmitter

lastly, you can use EventEmitter.emit(EventName, EventData) to emit custom events and receive them with EventEmitter.on(EventName) .最后,您可以使用EventEmitter.emit(EventName, EventData)发出自定义事件并使用EventEmitter.on(EventName)接收它们。

// import EventEmitter from "node:events"
class FireBase extends EventEmitter {
    constructor(client) {
         this.client = client;
     }

    login() {
        try {
            if(!firebaseConfig || !firebaseConfig.credential || !firebaseConfig.databaseURL){
               throw ('config data must be added to connect to the database\n [credential], [databaseURL] in config.json')
            }
            this.app  = initializeApp({ credential: cert(firebaseConfig.credential), databaseURL: firebaseConfig.databaseURL });
            this.db  = getFirestore(this.app);
            
            // Emit Event
            this.emit("login");
        } catch (err) {
            this.emit("error", err);
            throw new Error(red(err)).message
        }
    }
}
const fb = new Firebase(client);
fb.login();

// Receive Event
fb.on("login", () => {
   console.log('Logged in!');
});

fb.on("error", err => {
   console.error(`An error occured: ${err}`);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM