【问题标题】:setTimeout in NodeJS /Express throws TypeError: Cannot read property 'client' of undefinedNodeJS / Express中的setTimeout抛出TypeError:无法读取未定义的属性'client'
【发布时间】:2021-04-04 13:05:54
【问题描述】:

我在 nodejs 中有以下基本的快速路由:

app.get('/timeout', function (req, res) {
    setTimeout(() => {
      console.log('timeout');
    }, 2000);

    res.send({ hello: "world" });
  });

不行,报错

TypeError:无法读取未定义的属性“客户端” 在 setTimeout (/Users/myproject/node_modules/@sendgrid/mail/src/classes/mail-service.js:58:10) 在 /Users/myproject/router.js:87:7

我不明白为什么?我试过不做console.log,我试过箭头函数,异步等待,不发送res.send。这里的客户端是什么意思?

让我感到困惑的另一件事是错误的第一行提到了一个 sendgrid 模块——我确实安装了它,但我根本没有在这条路线中使用它——:at setTimeout (/Users/myproject/node_modules/@sendgrid/mail/src/classes/mail-service.js:58:10)

【问题讨论】:

    标签: javascript node.js express typeerror settimeout


    【解决方案1】:

    很抱歉,我发现了错误!我的 VS-Code 自动导入来自 sendgrid 的 setTimeout 函数。这不是标准的 setTimeout 函数,所以它抛出了一个错误。没看到它自动导入这个。

    const { setTimeout } = require('@sendgrid/mail');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-09
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 2022-11-29
      相关资源
      最近更新 更多