typescript 类型错误:Reflect.getMetadata不是函数

8ljdwjyq  于 2023-02-10  发布在  TypeScript
关注(0)|答案(3)|浏览(350)

下面是我启动nest服务器时遇到的问题:

back-end/node_modules/@nestjs/common/decorators/core/optional.decorator.js:22
                const args = Reflect.getMetadata(constants_1.OPTIONAL_DEPS_METADATA, target) || [];
                                     ^

TypeError: Reflect.getMetadata is not a function
        at /Users/lucabento-versace/OneDrive-Ynov/Projets/3eme_annee/WEB/TrueEat/back-end/node_modules/@nestjs/common/decorators/core/optional.decorator.js:22:34
        at /Users/lucabento-versace/OneDrive-Ynov/Projets/3eme_annee/WEB/TrueEat/back-end/node_modules/@nestjs/common/node_modules/tslib/tslib.js:104:41
        at Object.__decorate (/Users/lucabento-versace/OneDrive-Ynov/Projets/3eme_annee/WEB/TrueEat/back-
end/node_modules/@nestjs/common/node_modules/tslib/tslib.js:99:99)
        at Object.<anonymous> (/Users/lucabento-versace/OneDrive-Ynov/Projets/3eme_annee/WEB/TrueEat/back-end/node_modules/@nestjs/common/services/console-logger.service.js:204:43)
        at Module._compile (internal/modules/cjs/loader.js:1085:14)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
        at Module.load (internal/modules/cjs/loader.js:950:32)
        at Function.Module._load (internal/modules/cjs/loader.js:790:12)
        at Module.require (internal/modules/cjs/loader.js:974:19)
        at require (internal/modules/cjs/helpers.js:93:18)

我徒劳地测试了一下:npm i reflect-metadata我已经查看了关于同一错误的问题,但找不到任何解决方案

3htmauhk

3htmauhk1#

正如user @hhearts在上面的评论中提到的,通常通过将“reflect-metadata”导入到全局名称空间中来解决这个问题(这样您只需执行一次。
但您可以根据需要在范围内执行此操作。
例如,对于(p)react,您可以在App.ts中的第一次导入时执行此操作

import "reflect-metadata";

export const App: FunctionalComponent = () => {
    (...)
}
hpxqektj

hpxqektj2#

@nestjs/common/pipes导入ValidationPipe,而不是从@nestjs/common/pipes/validation.pipe导入

import { ValidationPipe } from '@nestjs/common/pipes';
ny6fqffe

ny6fqffe3#

对于我来说,我把import { ValidationPipe } from '@nestjs/common/pipes'改成了import { ValidationPipe } from '@nestjs/common',现在它可以工作了

相关问题