0

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?

sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
eggyolk
  • 1
  • 1
  • What exactly you're trying to do with the last line?? fs.appendFile(logger.log('Hello World!')) Also can you please share the logger file? – Arman Dec 17 '22 at 12:06
  • https://nodejs.org/api/fs.html#fsappendfilepath-data-options-callback Check this link – Arman Dec 17 '22 at 12:32

2 Answers2

0

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!')
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
0
fs.appendFile(logger.log('Hello World!'))

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

Tyler2P
  • 2,324
  • 26
  • 22
  • 31