Home > OS >  how to log data into a new file?
how to log data into a new file?

Time:12-18

I'm a beginner to node.js, started to learn just a few days ago. i'm trying to log the listener's data into a new file by using fs.appendFile, but no matter how many times i try to change the code, it keeps giving me an ERR-INVALID-CALLBACK.

const Logger = require('./logger_demo')
const logger = new Logger
const fs = require('fs')
logger.on('message', data => console.log('Called Listener: ', data))
fs.appendFile('./log_demo.js', 'message', (err) => {
  if (err) throw err
  console.log('File has been appended!')
})

fs.appendFile(logger.log('Hello World!'))

I'm not sure what I'm doing wrong, any idea how to solve this?

CodePudding user response:

fs.appendFile(logger.log('Hello World!'))

this line is your problem you are calling fs.appendFile with wrong parameters

CodePudding user response:

I think the issue may be here:

const logger = new Logger

Should it be this?

const logger = new Logger()

Additionally, you need to give a file-path to fs.appendFile:

fs.appendFile("log.txt", logger.log('Hello World!'))

But I think you meant to do this:

logger.log('Hello World!')
  • Related