javascript - 为什么 Node.js 中未定义函数
问题描述
你能帮我说说这个简单的网络服务器有什么问题吗?为什么在这一行中声明和回调之后validateWebhook
是正确的?以及如何解决这个问题?undefined
const result = validateWebhook(req.body);
看起来有问题,我错过了一些东西,但不明白出了什么问题。
const express = require('express');
const fs = require('fs');
const Ajv = require('ajv');
const port = process.env.PORT || 3000;
const app = express();
/* Create validateWebHook function */
/* This function validates input JSON supplied to webhook */
const validateWebhook = ((filePath) => {
fs.promises.readFile(filePath, {options: 'utf8'})
.then((data) => {
console.log(`Validation schema [${filePath}]:`);
console.log(`${data}`);
const ajv = new Ajv();
const result = ajv.compile(data);
console.log(`Type is ${typeof result}`);
return result;
})
.catch((error) => {
console.log(`Error loading json schema [${filePath}]`);
console.log(`Details: [${error}]`);
});
})('./schemas/waboxapp.json');
console.log(`Type is ${typeof validateWebhook}`);
app.post('/', (req, res) => {
console.log(`Input JSON: ${req.body}`);
console.log(`Type is ${typeof validateWebhook}`);
const result = validateWebhook(req.body);
console.log(`Validation result: ${result}`);
res.sendStatus(200);
});
app.listen(port, () => {
console.log(`Server is up on port ${port}`);
});
这是 JSON 模式:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://lookin.im/schemas/input/waboxapp.json",
"type": "object",
"properties": {
"event": { "type": "string" },
"token": { "type": "string" },
"uid": { "type": "string" },
"contact": { "$ref": "#/definitions/contact" },
"message": { "$ref": "#/definitions/message" }
},
"required": [ "event", "token", "uid", "contact", "message" ],
"additionalProperties": false,
"definitions": {
"contact": {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://lookin.im/schemas/input/contact.json",
"type": "object",
"properties": {
"uid": { "type": "string" },
"name": { "type": "string" },
"type": { "type": "string" }
},
"required": [ "uid", "name", "type" ],
"additionalProperties": false
},
"message": {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://lookin.im/schemas/input/message.json",
"type": "object",
"properties": {
"dtm": { "type": "string" },
"uid": { "type": "string" },
"cuid": { "type": "string" },
"dir": { "type": "string" },
"type": { "type": "string" },
"ack": { "type": "string" },
"body": {
"type": "object",
"properties": {
"text": { "type": "string" }
},
"required": [ "text" ],
"additionalProperties": false
}
},
"required": [ "dtm", "uid", "cuid", "dir", "type", "ack", "body" ],
"additionalProperties": false
}
}
}
我发送的 HTTP 请求是为了表达 js:
POST / HTTP/1.1
Host: localhost:3000
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
Postman-Token: 5d96ffe2-8f5f-477c-bc82-cd53147208c1
event=message&token=a09c8f3&uid=1&contact%5Buid%5D=1&contact%5Bname%5D=Name&contact%5Btype%5D=user&message%5Bdtm%5D=1&message%5Buid%5D=1&message%5Bcuid%5D=&message%5Bdir%5D=i&message%5Btype%5D=chat&message%5Bbody%5D%5Btext%5D=Test&message%5Back%5D=3
这是如何使用 curl 实用程序发送此请求:
curl -X POST \
http://localhost:3000/ \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Postman-Token: c319bdbe-16a9-42ab-a96b-2262e0c1fd81' \
-d 'event=message&token=a09c8f3&uid=1&contact%5Buid%5D=1&contact%5Bname%5D=Name&contact%5Btype%5D=user&message%5Bdtm%5D=1&message%5Buid%5D=1&message%5Bcuid%5D=&message%5Bdir%5D=i&message%5Btype%5D=chat&message%5Bbody%5D%5Btext%5D=Test&message%5Back%5D=3'
注意:我使用的是最新版本的依赖项。
{
"name": "test",
"version": "0.0.1",
"description": "Test",
"main": "app.js",
"author": "",
"license": "ISC",
"dependencies": {
"ajv": "^6.5.2",
"express": "^4.16.3"
}
}
节点版本是v10.5.0。
解决方案
挖出代码的内部部分以突出问题,这是您编写的内容:
const validateWebhook = ((filePath) => {
// Stuff, with no return statement
})('./schemas/waboxapp.json');
这是一个立即调用的函数表达式。它会立即调用该(filepath) => {}
函数,传入“./schemas/waboxapp.json”,然后将返回的任何内容分配给 validateWebhook。但是你不返回任何东西,所以它被设置为未定义。
推荐阅读
- kotlin - 操作系统组作为参数
- awk - 链 awk 正则表达式匹配,如 grep
- text - 软件工程文本的情感分析
- python - 我想在 python 上实现 Modified Gram-schmidt
- java - 在 Android 中插入 datetime.now SQLite
- python - '[5, 6, 7] 不一定与 list((5, 6, 7)) 相同?
- swift - UItextfield 文本覆盖 Swift 中文本字段中的清除按钮
- python - 在创建数组数组时与 numpy 广播不一致的行为
- c# - 如何在 C# 中获取多维数组的最小数量?
- typescript - 基于另一种类型的复合类型