[英]issue in understanding in `TypeError [ERR_INVALID_CALLBACK]: Callback must be a function` in node
i am new to node and having issue in understanding why this error TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
is occurs by running the command node file-create-directory.js
我是Node的新手,并且在理解为什么出现此错误
TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
遇到问题TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
通过运行命令node file-create-directory.js
发生TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
file-create-directory.js
file is: file-create-directory.js
文件是:
const fs = require('fs');
if(!fs.exists('views')){
fs.mkdir('views',(err) =>{
if(err) return err;
fs.writeFile('./views/new.html','this is a new directory and data', (err) =>{
if(err) return err;
console.log('Directory and file saved');
});
});
}
First of all fs.exists
is deprecated 首先不推荐使用
fs.exists
const fs = require('fs');
fs.access('views', fs.constants.F_OK, (err) => {
// if it does not exists
if(err) {
fs.mkdir('views', (err) => {
if(!err) {
fs.writeFile('./views/new.html', 'this is a new directory and data',
(err) => {
if(err) return err;
console.log('Directory and file saved');
}
});
}
});
But if you decide to use fs.exists
use this 但是,如果您决定使用
fs.exists
使用此选项
fs.exists('views', (exists) => {
if(!exists) {
fs.mkdir('views',(err) => {
if(err) return err;
fs.writeFile('./views/new.html','this is a new directory and
data', (err) => {
if(err) return err;
console.log('Directory and file saved');
});
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.