【问题标题】:TypeError `[ERR_INVALID_CALLBACK]`: On NodeJsTypeError `[ERR_INVALID_CALLBACK]`:在 NodeJs 上
【发布时间】:2021-11-28 21:07:15
【问题描述】:

收到 undefined throw new ERR_INVALID_CALLBACK(cb);

const fs = require("fs");
const text = "File ";

fs.writeFile("node-message.txt", text)
  .then(() => {
    console.log("File Created");
  })
  .catch(() => {
    console.log("File not created");
  });

我收到这个 TypeError [ERR_INVALID_CALLBACK]: Callback must be a function。

【问题讨论】:

    标签: node.js express fs


    【解决方案1】:

    如果您遇到无效的回调类型错误,您必须要求带有承诺的fs module。

        const fs = require("fs").promises;
        const text = "File ";
        
        fs.writeFile("node-message.txt", text)
          .then(() => {
            console.log("File Created");
          })
          .catch(() => {
            console.log("File not created");
          });
    

    【讨论】:

      猜你喜欢
      • 2021-03-10
      • 2018-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-28
      • 2015-12-24
      • 2021-01-17
      相关资源
      最近更新 更多