0

当我运行这个命令时curl -vd '{"NickName":"Marry","Password":"pwd"}' -H "Content-type: application/json" http://127.0.0.1:3000/signin,我 No metadata found. There is more than once class-validator version installed probably. You need to flatten your dependencies.在服务器端打印,并且validate没有正确执行。在这之前,我npm install有一些依赖:

npm install --save routing-controllers
npm install --save class-transformer
npm install --save class-validator

有人可以帮忙吗?我该如何解决这个问题?谢谢!

import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, UpdateDateColumn } from 'typeorm';
import { Length } from 'class-validator';
import * as ErrorCode from '../error/errorcode'

@Entity()
export class User {
    @PrimaryGeneratedColumn()
    @Column({
        name: "id"
    })
    Id?: number;

    @Column({
        name: "nickname"
    })
    @Length(1, 20, {
        message: "NickName must be 1 to 20 characters",
        context: {
            errorCode: ErrorCode.ParamLengthNotInRange
        }
    })
    Nickname: string;

    @Column({
        name: "password"
    })
    @Length(6,20)
    Password: string;

    //constructor(input : { Id : number , Nickname: string, Password: string}){
    constructor(input : { Nickname: string, Password: string}){
        //this.Id = input.Id;
        this.Nickname = input.Nickname;
        this.Password = input.Password;
    }

}

import { JsonController, Post, Body, Req } from "routing-controllers";
import { validate, ValidationError } from 'class-validator';
import {User} from '../entity'

@JsonController()
export class UserController {

    @Post('/signin')
    async signin(@Body() user: User) {
        const errors: ValidationError[] = await validate(user)
        if (errors && errors.length > 0) {
            console.log(errors[0].contexts!['Length'].errorCode)
        }
        console.log(user)
        return 'this is signin'
    }
}

4

2 回答 2

1

我们有同样的信息。从 0.12.2更新class-validator到 0.13.1 解决了这个问题。

于 2021-03-02T12:15:01.327 回答
1

您可以通过执行以下操作检查是否class-validator安装了更多版本:

npm ls class-validator

就我而言,我正在使用type-graphql并发现它type-graphql也在安装早期版本的class-validator. 所以我只是将 package.json 中的那个降级到早期版本以使它们匹配然后进行全新安装并且消息消失了。

希望这可以帮助。

于 2020-03-30T16:15:00.540 回答