简体   繁体   English

类型错误`[ERR_INVALID_CALLBACK]`:在 NodeJs 上

[英]TypeError `[ERR_INVALID_CALLBACK]`: On NodeJs

Received undefined throw new ERR_INVALID_CALLBACK(cb);接收到未定义抛出新的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");
  });

I am getting this TypeError [ERR_INVALID_CALLBACK] : Callback must be a function.我收到此 TypeError [ERR_INVALID_CALLBACK] :回调必须是 function。

You have to require an fs module with promises if you are facing an invalid callback type error.如果您遇到无效的回调类型错误,则必须 require 一个带有 promises 的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");
      });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM