【问题标题】:fs.writefile is not a function / Typescript / JSON [closed]fs.writefile 不是函数 / Typescript / JSON [关闭]
【发布时间】:2021-03-08 16:54:21
【问题描述】:

每当我尝试将数据写入 .json 文件时,都会出现错误。它说 'fs.writefile' 不是函数。

我的功能:

const fs = require('fs');


function write(data: Map<string, number>) {
    fs.writefile("users.json", JSON.stringify(data), function(err: any) {
        if (err) throw err;
        console.log('complete');
        }
    );
}

错误: (node:4408) UnhandledPromiseRejectionWarning: TypeError: fs.writefile is not a function

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: node.js json typescript writefile


    【解决方案1】:

    Javascript 属性名称区分大小写。 writeFile 方法的 F 是大写的,但 writefile 是小写的 f

    见:https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback

    【讨论】:

      猜你喜欢
      • 2018-09-03
      • 2021-05-29
      • 2021-08-18
      • 2022-12-19
      • 2021-04-28
      • 2019-10-26
      • 2021-11-04
      • 2020-04-02
      • 2019-01-22
      相关资源
      最近更新 更多