简体   繁体   English

如何修复此错误 TypeError [ERR_INVALID_CALLBACK]: Callback must be a function

[英]how to fix this error TypeError [ERR_INVALID_CALLBACK]: Callback must be a function

I am a beginner to the nodejs.我是 nodejs 的初学者。 When I type the below, the code error occurs like this:当我输入以下内容时,代码错误如下:

TypeError [ERR_INVALID_CALLBACK]: Callback must be a function TypeError [ERR_INVALID_CALLBACK]: 回调必须是一个函数

var fs = require('fs');
fs.readFile('readMe.txt', 'utf8', function (err, data) {
  fs.writeFile('writeMe.txt', data);
});

Fs.writeFile() according to the documentation here takes ( file, data[, options]and callback ) params so your code will be like this : Fs.writeFile() 根据此处的文档采用(文件、数据[、选项]和回调)参数,因此您的代码将如下所示:

 var fs = require('fs');
 fs.readFile('readMe.txt', 'utf8', function (err, data) {
  fs.writeFile('writeMe.txt', data, function(err, result) {
     if(err) console.log('error', err);
   });
 });

fs.writeFile(...) requires a third (or fourth) parameter which is a callback function to be invoked when the operation completes. fs.writeFile(...)需要第三个(或第四个)参数,这是在操作完成时调用的回调函数。 You should either provide a callback function or use fs.writeFileSync(...)您应该提供回调函数或使用fs.writeFileSync(...)

See node fs docs for more info.有关更多信息,请参阅节点 fs 文档

Since node 10, it is mandatory to pass a callback on fs.writefile()从节点 10 开始,必须在fs.writefile()上传递回调

Node.js documented the purpose for the change: https://github.com/nodejs/node/blob/master/doc/api/deprecations.md#dep0013-fs-asynchronous-function-without-callback Node.js 记录了更改的目的: https : //github.com/nodejs/node/blob/master/doc/api/deprecations.md#dep0013-fs-asynchronous-function-without-callback

You could add an empty callback like this fs.writeFile('writeMe.txt', data, () => {})你可以添加一个像这样的空回调fs.writeFile('writeMe.txt', data, () => {})

你也这样用

var file2 =  fs.readFileSync("./Public/n2.jpeg")

This error hit me in the face when I was doing the following;当我执行以下操作时,这个错误击中了我;

var hello = myfunction( callme() );

rather than而不是

var hello = myfunction( callme );

var fs = require('fs');

fs.readFile('readme.txt', 'utf8', function(err, data) {
    fs.writeFile('writemeee.txt', data, function(err, result) {

        if (err) console.log('error', err);

    });
});

You simply could use the sync function您只需使用同步功能

var fs = require('fs');
fs.readFileSync('readMe.txt', 'utf8', function (err, data) {
  fs.writeFileSync('writeMe.txt', data);
});

or use callback function或使用回调函数

you can import the fs module from the fs/promises as they are promise-fied version of the modules so we don't need to use the callback function unnecessarily.您可以从 fs/promises 导入 fs 模块,因为它们是模块的承诺版本,因此我们不需要不必要地使用回调函数。

import fs from 'fs/promises';

fs.readFileSync('readMe.txt', 'utf8', function (err, data) {
fs.writeFileSync('writeMe.txt', data);`});`

try this .I have written the code using Promises.试试这个。我已经使用 Promises 编写了代码。

const {readFile} = require('fs');
const {writeFileSync} = require('fs');
const readText = (path)=>{
 return new Promise((resolve,reject) => {
    readFile(path,'utf8',(err,result)=>{
      if(err)
        reject(err);
      else
        resolve(result);
      })
  })
}

readText('./contents/sample.txt')
  .then(val=>writeFileSync('./results.txt',val))
  .catch(err=>console.log(err));

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

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