TypeError [ERR_INVALID_CALLBACK]:回调必须是一个函数。在nodejs中收到未定义

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

大家好,我是 node js 的新手,我在尝试将我的文件附加到 node js 时遇到错误不知道发生了什么问题请尝试修复我的错误并告诉我我应该怎么做?

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);

你的 fs.appendFile 函数应该有一个回调函数作为第三个参数。

语法:

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

您的 JS 文件应如下更改:

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);

有关 fs.appendFile 的更多信息:https://nodejs.org/api/fs.html#fs_fs_appendfile_path_data_options_callback