tslint Error - Shadowed name: 'err'

bobdolan picture bobdolan · May 28, 2018 · Viewed 32.8k times · Source

tslint is currently throwing the following error

Shadowed name: 'err'

Here is the code

fs.readdir(fileUrl, (err, files) => {
        fs.readFile(path.join(fileUrl, files[0]), function (err, data) {
            if (!err) {
                res.send(data);
            }
        });
    });

Anybody have a clue on what the best way would be to solve this and what the error even means?

Answer

Padmapriya Vishnuvardhan picture Padmapriya Vishnuvardhan · May 28, 2018

You are using the same variable "err" in both outer and inner callbacks, which is prevented by tslint.

If you want to use the same variable then "no-shadowed-variable": false, otherwise do as below.

fs.readdir(fileUrl, (readDirError, files) => {
    fs.readFile(path.join(fileUrl, files[0]), function (err, data) {
            if (!err) {
                res.send(data);
            }
        });
    });