Javascript 缺少回调函数

Javascript 缺少回调函数,javascript,node.js,Javascript,Node.js,我想读取其中的目录和文件,所以我编写了这段代码并运行它。在运行时,我发现fs:missing回调函数:enoint readdir'D:\dev\corpdashboboard\dashboard.js' “D:\dev\corpdashboboard”是我存放ejs文件和js文件的地方 这意味着什么? 要从官方规范中读取目录和文件,我应该做什么 你使用了错误的论点fs.readdir()不接受编码。所以readdir需要path这是一个字符串,而callback这是一个函数 应该是: fs.r

我想读取其中的目录和文件,所以我编写了这段代码并运行它。在运行时,我发现fs:missing回调函数:enoint readdir'D:\dev\corpdashboboard\dashboard.js' “D:\dev\corpdashboboard”是我存放ejs文件和js文件的地方 这意味着什么? 要从官方规范中读取目录和文件,我应该做什么

你使用了错误的论点
fs.readdir()
不接受编码。所以
readdir
需要
path
这是一个字符串,而
callback
这是一个函数

应该是:

fs.readdir(path, callback)
Asynchronous readdir(3). Reads the contents of a directory.
The callback gets two arguments (err, files) where files is
an array of the names of the files in the directory excluding '.' and '..'.

任何阅读本文的人都要注意:上面的答案是有效的,但是在NodeV6.x中,fs.readdir接受一个额外的参数,您可以在文档中看到


如果遇到此问题,请检查以确保运行的是Node v6.0.0或更高版本。

删除第二个参数(“UTF-8”)。如果希望其他人阅读您的代码,请正确缩进。这应该是对主题答案的注释。^我没有足够的代表。
fs.readdir(path, callback)
Asynchronous readdir(3). Reads the contents of a directory.
The callback gets two arguments (err, files) where files is
an array of the names of the files in the directory excluding '.' and '..'.
var data = fs.readdir('corpdashboard/dashboards',function (err, files) {
    if (err) {
         return console.log(err);
    }
    console.log(data);
});