【发布时间】:2020-10-05 15:53:51
【问题描述】:
我无法将对象传递给从另一个模块导出的函数。故事是这样的。
我正在从模块 TagService.js
导出一个函数文件:TagService.js
addTag = ({tag}) => {
//some activity
}
module.exports = { addTag, //other functions}
从模块ServiceHandler.js
调用函数文件:ServiceHandler.js
const Controller = require('./Controller');
const service = require('../services/TagService');
const addTag = async (request, response) => {
await Controller.handleRequest(request, response, service.addTag);
};
以下是 Controller.js
中控制器的结构文件:Controller.js
static async handleRequest(request, response, serviceOperation) {
//some activity
const serviceResponse = await serviceOperation(this.collectRequestParams(request));
//some more activity...
}
static collectRequestParams(request) {
//some activity
return requestParams;
}
现在,在 Controller 中,requestParams 已成功返回。但是当调用进入 TagService 的 addTag 函数时,对象 tag 没有被传递!
多一点背景。这是从 openapi-generator 为 nodejs-express-server 存根生成的代码。
这是标签服务的 openapi.yaml 模板。
/samyojya-tag:
post:
operationId: addTag
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Tag'
responses:
"201":
content:
...
schemas:
Tag:
example:
name: name
id: 1
type: type
properties:
id:
type: string
name:
type: string
category:
type: string
type: object
xml:
name: Tag
使用 node 12.16.2 和 express 4.16.1
【问题讨论】:
-
这意味着在
TagService.js中你不能在你的函数addTag中访问对象? -
是的。那是正确的。 @Ifaruki
标签: javascript node.js express scope openapi-generator