Node help! Why this require works inside arrow function but no outside of it?

General Discussion
  • I'm geting DB is not a constructor for the following code. If I move the const 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;
    
  • FWIW, I should mention that this is a forum for community discussion for NodeBB, the forum software, not for Node.js 😄

  • @babaliaris who is calling the logger.mysql () function... probably a browser.. but the CONST DB was done in the main process I would guess..


Suggested Topics


  • How do groups work

    General Discussion
    0 Votes
    3 Posts
    17 Views

    We're quite literal about it. A group is literally just a collection of users. It doesn't afford them anything special beyond a badge.

    However, because it's quite low level, you can do a lot with groups. The privilege system works with groups, so you are able to restrict access to some/all categories based on user groupings.

  • 0 Votes
    19 Posts
    664 Views

    Thanks for the effort. @PitaJ Its okay. 👍

  • Need your help

    General Discussion
    0 Votes
    2 Posts
    636 Views

    If you want a completely empty home page, I suggest using the custom pages plugin and pointing your home page setting in the ACP to a blank custom page.

    Otherwise, you can use css

  • 0 Votes
    20 Posts
    4k Views

    and MongoDB tells:
    [Thread1] SyntaxError: Missing ; before statement @<shell>:1:10

  • PHP doesnt work

    General Discussion
    -2 Votes
    9 Posts
    2k Views

    The fun part should be learning NodeBB and Nodejs. Start learning the basics and then ask specific questions in this forum 😉 Keep learning to be happy 😛