0
function onRequest(req, res) {
    if (req.body.widget_name) {
        console.log(req.body.widget_name);
    }
    var fs = require('fs');
    var data = fs.readdir("corpdashboard/dashboards", 'UTF-8', function (err, files) {
        if (err) {
            return console.log(err);
        }
        console.log(data);
    });
    var body = req.body.dashboard_selected + ' ' + req.body.widget_selected + ' ';
    res.setHeader('Content-Type', 'text/plain');
    res.setHeader('Content-Length', body.length);
    res.end(body);
}
exports.onrequest = onRequest;

I want to read directories and the file in that so I write this code and run it. on running i am getting that fs:missing callback function :ENOENT readdir 'D:\dev\corpdashboboard\dashboard.js' 'D:\dev\corpdashboboard' is the where i have my ejs file and js file what does it means? what should I do to read directories and files in them

3
  • 1
    fs.readdir(path, callback) Commented Nov 28, 2013 at 8:23
  • 1
    Remove the second argument ('UTF-8'). Commented Nov 28, 2013 at 8:26
  • 1
    If you expect other people to read your code, please indent it properly. Commented Nov 28, 2013 at 8:50

2 Answers 2

1

From official spec: http://nodejs.org/api/fs.html#fs_fs_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 '..'.

You are using wrong arguments. fs.readdir() does not take encoding. So readdir expects path which is a string and callback which is a function.

It should be:

var data = fs.readdir('corpdashboard/dashboards',function (err, files) {
    if (err) {
         return console.log(err);
    }
    console.log(data);
});
Sign up to request clarification or add additional context in comments.

Comments

0

A note to anyone reading this: The answers above were valid, but in Node v6.x, fs.readdir does accept an additional argument, which you can see in the documentation. https://nodejs.org/api/fs.html#fs_fs_readdir_path_options_callback

If you're encountering this issue, check to make sure that you're running Node v6.0.0 or later.

2 Comments

This should be a comment on the subject answer.
^ I've not enough rep.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.