node.js - 'err' 已在上层范围内声明
问题描述
我的代码没有给出任何错误,只是我必须满足 eslint,因为它给出的错误 'err' 已经在上层范围内声明。 如何在我的代码中修复它。
describe('/GET/:ID', () => {
it('should Get the task by ID', (done) => {
const book = new Task({ task: 'The Lord of the Rings' });
book.save((err, task) => {
chai.request(server)
.get(`/task/${task.id}`)
.send(task)
.end((err, res) => {
expect(res).to.have.status(200);
done();
});
});
});
});
解决方案
ESlint 警告你变量 shadowing,不是因为它会抛出错误或不起作用,只是因为有时它可能是无意的,从而导致意外的行为,例如你想在回调中使用err
from ?book.save
chai
要修复,只需使用更明确的命名约定,例如saveErr
/chaiErr
推荐阅读
- google-cloud-platform - 无法禁用 GCP API 证书颁发机构服务
- flask - 通过 pythonanywhere 代理路由 Flask 应用程序 API GET 请求
- image - 使用和不使用 NI Vision 从二进制缓冲区/文件创建 LabVIEW IMAQ 图像
- angular - 包含二进制文件作为项目成员的角度上传对象数组
- python - 使用 TensorFlow/Keras 评估语言模型
- css - 如何在 create-react-app 中缩小 CSS 类?
- django - 为什么 django post 方法在这种情况下不起作用?
- vba - 使用 Web Broswer Control 从 URL 缩放图像
- javascript - 当我从数据库中获取图像文件路径时,React Native 不显示图像
- angular - 如何在 Angular 11 中使用 signalR 协商 Azure 函数在令牌到期后获取访问令牌?