[英]nodeJS returns Type Error when creating a file using fs writeFile
I've just started learning nodeJS.我刚开始学习nodeJS。 I'm at a very basic level.
我处于非常基础的水平。
Right now I'm working with creating new files and directories methods.现在我正在使用创建新文件和目录的方法。
This is my Code -这是我的代码 -
var fs = require('fs');
fs.mkdir("stuff", function () {
fs.readFile('readMe.txt', 'utf8', function (err, data){
fs.writeFile('./stuff/writeMe.txt', data);
});
});
It does create the directory and does read the file, but doesn't create a new file.它会创建目录并读取文件,但不会创建新文件。
I checked the code times and again, but the terminal is returning this error.我一次又一次地检查了代码,但终端返回了这个错误。
fs.js:152
throw new ERR_INVALID_CALLBACK(cb);
^
TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined
[90m at maybeCallback (fs.js:152:9)[39m
[90m at Object.writeFile (fs.js:1351:14)[39m
at D:\PRANAV\Learning Folder\Node\app.js:6:12
[90m at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:63:3)[39m {
code: [32m'ERR_INVALID_CALLBACK'[39m
}
I'm stuck at this, please help.我被困在这个问题上,请帮忙。
fs.writeFile
's last argument should be a callback function. fs.writeFile
的最后一个参数应该是一个回调 function。 Right now, you're missing it:现在,你错过了它:
fs.mkdir("stuff", function () {
fs.readFile('readMe.txt', 'utf8', function (err, data) {
fs.writeFile('./stuff/writeMe.txt', data, function (err) {
if (err) {
throw err;
}
console.log('written successfully!');
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.