首页 > 解决方案 > NodeJS / Express中的setTimeout抛出TypeError:无法读取未定义的属性'client'

问题描述

我在nodejs中有以下基本快递路线:

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

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

它不起作用,它会引发错误

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

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

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

标签: javascriptnode.jsexpresstypeerrorsettimeout

解决方案


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

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

推荐阅读