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?
When a variable in a local scope and a variable in the containing scope have the same name, shadowing occurs. Shadowing makes it impossible to access the variable in the containing scope and obscures to what value an identifier actually refers to.
Shadowing means declaring an identifier that has already been declared in an outer scope. Since this is a linter error, it's not incorrect per se, but it might lead to confusion, as well as make the outer i unavailable inside the loop (where it is being shadowed by the loop variable.)
In programming, shadowing occurs when a variable declared in a certain scope (e.g. a local variable) has the same name as a variable in an outer scope (e.g. a global variable). When this happens, the outer variable is said to be shadowed by the inner variable.
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);
}
});
});
Add this comment just above the error line--
// tslint:disable-next-line:no-shadowed-variable
This shadowed name tslint error is due to repetition of the name 'err' twice in your callback functions. You can change either anyone 'err' to other name so this should work.
Example: This should work
fs.readdir(fileUrl, (error, files) => {
fs.readFile(path.join(fileUrl, files[0]), function (err, data) {
if (!err) {
res.send(data);
}
});
});
When same variable is declared multiple times in same scope, this warning occurs.
Use different variable names in such cases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With