Node help! Why this require works inside arrow function but no outside of it?
-
I'm geting
DB is not a constructor
for the following code. If I move theconst DB = require("./db");
inside the mysql() arrow function then it works. But I can't understand why...const winston = require("winston"); const DB = require("./db"); let logger = winston.createLogger( { format: winston.format.json(), transports: [ new winston.transports.File({ filename: 'logs.txt'}), new winston.transports.Console({ format: winston.format.colorize()}) ] }); logger.mysql = (level, msg, filename, stack)=> { //Create the database object. const db = new DB(); //Open the connection. db.open().then((result)=> { db.conn.query(`INSERT INTO t_logs (m_level, m_msg, m_filename, m_stack) VALUES (?, ?, ?, ?);`, [level, msg, filename, stack], (err)=> { //Something went wrong. if (err) logger.error(err.stack); //Close the connection. db.close().catch((e)=>logger.error(e.stack)); }); }) .catch((e)=> { logger.error(e.stack); }); }; module.exports = logger;
-
@babaliaris who is calling the logger.mysql () function... probably a browser.. but the CONST DB was done in the main process I would guess..
Copyright © 2024 NodeBB | Contributors