TypeError: Cannot read property 'db' of null appears when node.js interacts with mongodb

I use a third-party package of huya-danmu to monitor the on-screen gift of Huya TV"s live studio
address: https://github.com/BacooTang/.
and store the on-screen gift information in mongodb. The logic is to obtain all stored addresses of Tiger Tooth studio by querying HUYA first, and then traverse the query result in the callback function. Execute the on-screen gift listening code one by one and store the results in mongo
, but there will always be an error of TypeError: Cannot read property "db" of null when the program is running. Sometimes this error will directly interrupt the program and throw an exception, and sometimes it will not be displayed in the log entered by me instead of the terminal program.

error message of interrupt program:

clipboard.png

error message in the log:
[2018-06-13T16:31:34.777] [DEBUG] default-MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect EADDRINUSE 127.0.0.1 purge 27017]

the following is the code that interacts with mongo:

client.on("message", msg => {
            switch (msg.type) {
                case "chat":
                    // mongodb
                    mongoclient.connect(danmu_con.mongo_url, function (err, db) {
                        if(err) {
                            logger.debug(`${err}`);
                        };
                        var dbo = db.db("danmu");
                        var data = {anchor_id: `${id}`, live_url: `${element}`, platform_id: 7, add_time: parseInt(`${time}`), type: "danmu", content: `${msg.content}`};
                        dbo.collection("huya").insertOne(data, function (err, result) {
                            if (err) {
                                logger.debug(`${err}`);
                            };
                            // console.log("");
                            db.close();
                        });
                    });
                    // console.log(`[${msg.from.name}]:${msg.content}`);
                    break;
                case "gift":
                    // mongodb
                    mongoclient.connect(danmu_con.mongo_url, function (err, db) {
                        if(err) {
                            logger.debug(`${err}`);
                        };
                        var dbo = db.db("danmu");
                        var data = {anchor_id: `${id}`, live_url: `${element}`, platform_id: 7, add_time: parseInt(`${time}`), type: "gift", content: `${msg.name}`, gift_num: `${msg.count}`};
                        dbo.collection("huya").insertOne(data, function (err, result) {
                            if(err) {
                                logger.debug(`${err}`);
                            };
                            // console.log("");
                            db.close();
                        });
                    });
                    // console.log(`[${msg.from.name}]->${msg.count}${msg.name}`);

                    // xj_gift_value
                    dbhelper.Query(format(select_sql, msg.name, huya_id), function (err, rows) {
                        if (err) {
                            logger.debug(err);
                        };
                        if (rows.length == 0) {
                            dbhelper.Query(format(insert_sql, msg.id, msg.name, huya_id, parseFloat(msg.earn/msg.count), time, time), function (err, rows) {
                                if (err) {
                                    logger.debug(err);
                                }else{
                                    logger.debug("insert successfully!");
                                };
                            });
                        };
                    });
                    break;
                case "online":
                    // console.log(`[]:${msg.count}`);
                    break;
            };
        });

I hope you can answer my questions. Thank you very much!

Mar.19,2021

error message shows that 127.0.0.1 conncet mongodb 27017 has been occupied. I guess this phenomenon is caused by repeating conncet mongodb. It is suggested that one way is to put all logic in the connect big callback, and the other is to use connection pooling

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bf1d77-31530.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bf1d77-31530.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?