node.js - 无法使用 Node JS v10.14.1 os 模块中的 userInfo() 函数

标签 node.js node-modules

我收到错误的代码是:

console.log('Starting app.js...');
const fs = require('fs');
const os = require('os');
var user = os.userInfo();
fs.appendFile('Abhisht.txt', 'Hii ${user.username}!');

我收到的命令和错误是:

C:\Users\Abhisht Srivastava\Desktop\nodeapp>node app.js
Starting app.
fs.js:129
  throw new ERR_INVALID_CALLBACK();
  ^

TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
at maybeCallback (fs.js:129:9)
at Object.appendFile (fs.js:1210:14)
at Object.<anonymous> (C:\Users\Abhisht Srivastava\Desktop\nodeapp      \app.js:8:4)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
at startup (internal/bootstrap/node.js:285:19)

我使用的是 Node v10.14.1。

最佳答案

您应该添加回调。

您有一个文件操作,并且文件操作需要回调。

第5行,文件操作完成后,需要添加回调方法。

console.log('Starting app.js...');
const fs = require('fs');
const os = require('os');
var user = os.userInfo();
fs.appendFile('Abhisht.txt', `Hii ${user.username}!`, (err) => {
    console.log('Done');
});

请检查doc以获得更多选择。 另外,单引号 ' 需要替换为反引号 `

关于node.js - 无法使用 Node JS v10.14.1 os 模块中的 userInfo() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53720657/

相关文章:

node.js - 如何在 Elasticsearch 中索引 Mongoose 时间戳

node.js - 如何在我的 Electron 应用程序中打包knex以使用sqlite3

json - 如何在 Express 应用程序中使用 JSON POST 数据

node.js - webpack < 5 不包含 Node.js 核心模块的 Polyfill

http - 代理后面的 npm 失败,状态为 403

mysql - Nodejs到mysql连接错误

node.js - angular-cli:为什么当 npm 安装新模块时,旧模块会被删除

node.js - uuidv5 的 Typescript 声明,导出枚举和默认函数

javascript - 如何将命令行参数传递给嵌套脚本?

node.js - Bluebird - promisification - promisifyAll of 'email-templates' Node 模块 - 发送邮件