Reputation: 573
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?
Upvotes: 32
Views: 56442
Reputation: 8726
When same variable is declared multiple times in same scope, this warning occurs.
Use different variable names in such cases.
Upvotes: 3
Reputation: 61
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);
}
});
});
Upvotes: 3
Reputation: 2166
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);
}
});
});
Upvotes: 40
Reputation: 4915
Add this comment just above the error line--
// tslint:disable-next-line:no-shadowed-variable
Upvotes: 5
Reputation: 621
this line will disable your error,
// tslint:disable: no-shadowed-variable
but it's not okay to have tow err variables you can also change the second err variable name to something different
fs.readdir(fileUrl, (err, files) => {
fs.readFile(path.join(fileUrl, files[0]), function (readFileErr, data) {
if (!readFileErr) {
res.send(data);
}
});
});
I had an error like this interfaces.ts:119:26 - Shadowed name: 'POST'
// tslint:disable: no-shadowed-variable
interface API {
export namespace APINAME {
export type POST {
}
}
export namespace OTHERAPINAME {
export type POST {
}
}
}
I have disabled this error case with this line // tslint:disable: no-shadowed-variable
because sometimes typescript compiler cannot understand your code right :)
Upvotes: 0