TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined in nodejs
Asked Answered
D

1

9

Hi Guys am new in node js , i am getting an error when am trying to append my file in node js don't know what's going on wrong please try to fix my error and also tell me what should i do?

console.log("Starting app.js");

const fs = require('fs');
const os = require('os');
const notes = require('../notes-node/notes.js');

var user = os.userInfo();

fs.appendFile('message.txt', `Hello ${user.username}! You are ${notes.age}`)

console.log(user);
Disclose answered 28/11, 2020 at 6:4 Comment(0)
H
6

Your fs.appendFile function should have a callback function as the third parameter.

Syntax:

fs.appendFile( path, data[, options], callback )

Your JS File should change like this:

console.log("Starting app.js");

const fs = require('fs');
const os = require('os');
const notes = require('../notes-node/notes.js');

var user = os.userInfo();

fs.appendFile("message.txt", `Hello ${user.username}! You are ${notes.age}`, (err) => { 
  if (err) { 
    console.log(err); 
  } 
}); 

console.log(user);

More info on fs.appendFile : https://nodejs.org/api/fs.html#fs_fs_appendfile_path_data_options_callback

Haigh answered 28/11, 2020 at 6:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.