我正在尝试将swagger添加到我的nestjs应用程序中,这是我得到的一个错误。
(node:78477) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of undefined
at lodash_1.mapValues.param (/Users/anna/SoFetch/node_modules/@nestjs/swagger/dist/explorers/api-parameters.explorer.js:34:20)
at /Users/anna/SoFetch/node_modules/@nestjs/swagger/node_modules/lodash/lodash.js:13401:38
at /Users/anna/SoFetch/node_modules/@nestjs/swagger/node_modules/lodash/lodash.js:4905:15
at baseForOwn (/Users/anna/SoFetch/node_modules/@nestjs/swagger/node_modules/lodash/lodash.js:2990:24)
at Function.mapValues (/Users/anna/SoFetch/node_modules/@nestjs/swagger/node_modules/lodash/lodash.js:13400:7)
at exploreApiReflectedParametersMetadata (/Users/anna/SoFetch/node_modules/@nestjs/swagger/dist/explorers/api-parameters.explorer.js:33:41)
at exports.exploreApiParametersMetadata (/Users/anna/SoFetch/node_modules/@nestjs/swagger/dist/explorers/api-parameters.explorer.js:11:33)
at explorers.reduce (/Users/anna/SoFetch/node_modules/@nestjs/swagger/dist/swagger-explorer.js:55:45)
at Array.reduce (<anonymous>)
at lodash_1.mapValues (/Users/anna/SoFetch/node_modules/@nestjs/swagger/dist/swagger-explorer.js:54:97)
at /Users/anna/SoFetch/node_modules/@nestjs/swagger/node_modules/lodash/lodash.js:13401:38
at /Users/anna/SoFetch/node_modules/@nestjs/swagger/node_modules/lodash/lodash.js:4905:15
at baseForOwn (/Users/anna/SoFetch/node_modules/@nestjs/swagger/node_modules/lodash/lodash.js:2990:24)
at Function.mapValues (/Users/anna/SoFetch/node_modules/@nestjs/swagger/node_modules/lodash/lodash.js:13400:7)
at MapIterator.denormalizedPaths.metadataScanner.scanFromPrototype.name [as iteratee] (/Users/anna/SoFetch/node_modules/@nestjs/swagger/dist/swagger-explorer.js:54:45)
at MapIterator.next (/Users/anna/SoFetch/node_modules/iterare/lib/map.js:13:39)
at FilterIterator.next (/Users/anna/SoFetch/node_modules/iterare/lib/filter.js:12:34)
at IteratorWithOperators.next (/Users/anna/SoFetch/node_modules/iterare/lib/iterate.js:21:28)
at Function.from (<anonymous>)
at IteratorWithOperators.toArray (/Users/anna/SoFetch/node_modules/iterare/lib/iterate.js:180:22)
at MetadataScanner.scanFromPrototype (/Users/anna/SoFetch/node_modules/@nestjs/core/metadata-scanner.js:11:14)
at SwaggerExplorer.generateDenormalizedDocument (/Users/anna/SoFetch/node_modules/@nestjs/swagger/dist/swagger-explorer.js:48:56)
(node:78477) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:78477) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我把问题的范围缩小到了控制器。这就是它:
constructor(@Inject('UserService') private readonly userService: UserService) {}
@Post('login')
async login(@Body() login: LoginModel): Promise<any> {
return this.userService.login(login.username, login.password);
}
@Post('register/customer')
async registerCustomer(@Body() customer: CustomerRegisterModel): Promise<any> {
if(customer.password !== customer.confirmPassword) {
throw new NotAcceptableException();
}
return this.userService.createCustomer(customer.name, customer.username, customer.email, customer.password);
}
@Post('register/technician')
async registerTechnician(@Body() technician: TechnicianRegistrationModel): Promise<any> {
return this.userService.createTechnician(technician.name, technician.username, technician.email, technician.password, technician.location);
}
这个控制器不工作,但是当我添加一个像这样的简单方法时:
@Get()
Hello(login: string):string{
return "helloworld"
}
它起作用了。
看起来我在@Body @Query属性上遇到了问题。但是我找不到它。
其他人也有同样的问题吗?
发布于 2020-01-19 04:35:17
在添加@Body() ...
参数修饰器之后,我也遇到了同样的错误,最后,我用下面的代码替换了我的tsconfig.json
文件,从而解决了这个问题。
{
"compilerOptions": {
"module": "commonjs",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"allowSyntheticDefaultImports": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"sourceMap": true,
"allowJs": false,
"outDir": "./dist"
},
"include": [
"src/**/*"
],
"exclude": [
"**/node_modules/*",
"**/*.spec.ts"
]
}
发生了什么: swagger文件parameter-metadata-accessor.js
中抛出了一个错误,特别是types
数组是未定义的,所以当parametersWithType
访问它时,它抛出了。
class ParameterMetadataAccessor {
explore(instance, prototype, method) {
const types = Reflect.getMetadata(constants_1.PARAMTYPES_METADATA, instance, method.name);
const routeArgsMetadata = Reflect.getMetadata(constants_1.ROUTE_ARGS_METADATA, instance.constructor, method.name) || {};
const parametersWithType = lodash_1.mapValues(reverse_object_keys_util_1.reverseObjectKeys(routeArgsMetadata), (param) => ({
type: types[param.index],
name: param.data,
required: true
}));
...
}
https://stackoverflow.com/questions/58596139
复制相似问题