node.js - 'err' 已在上部范围中声明

标签 node.js express mocha.js eslint chai

我的代码没有给出错误,只是我必须满足 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 警告您 variable shadowing ,并不是因为它会抛出错误或不起作用,只是因为有时它可能是无意的,从而导致意外的行为,例如假设您希望在 chai 回调中使用 book.save 中的 err

要解决此问题,只需使用更明确的命名约定,例如saveErr/chaiErr

关于node.js - 'err' 已在上部范围中声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59841831/

相关文章:

css - 使用 stylus 解析时 bootstrap.styl 中断

reactjs - 连接组件的 React js 单元测试 - mocha

node.js - Mongoose 正在更改 unix 时间戳并将其转换为 UTC 时间

node.js - 如何处理 Dockerfile for dev/prod 的差异

node.js - Electron NODE_MODULE_VERSION 错误和重建的串行端口无法修复

reactjs - Express 未获取 React 构建文件夹

node.js - 在 GET 请求中使用 HTTPOnly Cookie(使用 user_id cookie 从服务器检索用户信息)

javascript - Mocha 测试后功能

node.js - 在 FeathersJS 中使用 connect-history-api-fallback 中间件来服务 Vue.js SPA

javascript - XD 插件中的 npm 包或 Node.js API