简体   繁体   中英

how to log data into a new file?

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?

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!')
fs.appendFile(logger.log('Hello World!'))

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM