【问题标题】:Event is not defined nodejs事件未定义nodejs
【发布时间】:2021-09-24 02:55:29
【问题描述】:

我从 patreon 收到一个 webhook,当我启动测试时,我收到了这个错误:

(node:636) UnhandledPromiseRejectionWarning: ReferenceError: event is not defined

这是我的代码

const express = require('express');
const bodyParser = require('body-parser');
const app = express();

app.post('/', async (req, res) => {
    let crypto = require('crypto');

    let hash = event.headers['X-Patreon-Signature'],
        hmac = crypto.createHmac("md5", 'patreon-secret-removed ;)'); 

    hmac.update(event.body);

    let crypted = hmac.digest("hex");

    if (crypted === hash) {
        console.log("That's good hash! " + hash);
    } else {
        console.log("Bad hash! " + hash + " crypted " + crypted);
    }
})
app.listen(8080)

如何替换或定义事件?

【问题讨论】:

  • 你的意思是写req.headers['X-Patreon-Signature']和req.body吗?
  • 现在我得到错误 (node:1009) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: "data" 参数必须是字符串类型或 Buffer、TypedArray 或 DataView 的实例。收到未定义
  • 那是因为你没有正确使用正文解析器。见stackoverflow.com/questions/10005939/…
  • 不错的复制和粘贴错误。 event 未定义,因为它未声明。我猜你的意思是 req 而不是 event...

标签: node.js webhooks patreon


【解决方案1】:

let hash = event.headers['X-Patreon-Signature']

也许这是一个请求,而不是一个事件。

【讨论】:

  • 现在我收到错误 现在我收到错误 (node:1009) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: "data" 参数必须是字符串类型或 Buffer、TypedArray 或 DataView 的实例.收到未定义 -
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-02
  • 1970-01-01
  • 1970-01-01
  • 2017-07-24
相关资源
最近更新 更多