【发布时间】: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...