繁体   English   中英

为什么在Node.js中未定义函数

[英]Why function is undefined in Node.js

您能帮我说一下这个简单的Web服务器有什么问题吗? 为什么在声明后和在此const result = validateWebhook(req.body);中的回调中立即undefined validateWebhook 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。 但是您什么也不返回,因此将其设置为undefined。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM