【问题标题】:Nestjs - QueryFailedError: invalid input syntax for type uuidNestjs - QueryFailedError:uuid 类型的输入语法无效
【发布时间】:2020-03-10 14:57:42
【问题描述】:

我有这个方法应该返回所有用户的所有帖子 基本上是这样Select * from posts

  /**
   * Find all posts 
   */
  async findAll(): Promise<Post[]> {
    try {
      return await await getConnection()
      .createQueryBuilder()
      .select("posts")
      .from(Post, "posts").getMany();
    } catch (error) {
      throw new Error(error)      
    }
  }

当这个方法被调用时,这是来自 TypeORM 的响应

QueryFailedError: invalid input syntax for type uuid: "findAll"

查询输出

SELECT DISTINCT
   "distinctAlias"."Post_post_id" as "ids_Post_post_id" 
FROM
   (
      SELECT
         "Post"."post_id" AS "Post_post_id",
         "Post"."uid" AS "Post_uid",
         "Post"."title" AS "Post_title",
         "Post"."sub_title" AS "Post_sub_title",
         "Post"."content" AS "Post_content",
         "Post"."userUserId" AS "Post_userUserId",
         "Post__comments"."comment_id" AS "Post__comments_comment_id",
         "Post__comments"."content" AS "Post__comments_content",
         "Post__comments"."postPostId" AS "Post__comments_postPostId",
         "Post__comments"."userUserId" AS "Post__comments_userUserId" 
      FROM
         "posts" "Post" 
         LEFT JOIN
            "comments" "Post__comments" 
            ON "Post__comments"."postPostId" = "Post"."post_id" 
      WHERE
         "Post"."post_id" = $1
   )
   "distinctAlias" 
ORDER BY
   "Post_post_id" ASC LIMIT 1

这是我的架构

帖子

/**
 * Post Entity
 */
@Entity('posts')
export class Post {
  @PrimaryGeneratedColumn('uuid') post_id: string;
  @Column({ type: 'varchar', nullable: false, unique: true }) uid: string;
  @Column('text') title: string;
  @Column('text') sub_title: string;
  @Column('text') content: string;
  @ManyToOne(
    type => User,
    user => user.posts,
    {
      cascade: true,
    },
  )
  user: User;
  @OneToMany(
    type => Comment,
    comment => comment.post,
    {
      cascade: true,
    },
  )
  comments: Comment[];

  constructor(title?: string, content?: string) {
    this.title = title || '';
    this.content = content || '';
  }

  @BeforeInsert() async generateUID() {
    this.uid = uuid();
  }
}

用户

/**
 * User Entity
 */
@Entity('users')
export class User {
  @PrimaryGeneratedColumn('uuid') user_id: string;
  @Column({ type: 'varchar', nullable: false, unique: true }) uid: string;
  @Column({ type: 'varchar', nullable: false }) name: string;
  @Column({ type: 'varchar', nullable: false, unique: true }) email: string;
  @Column({ type: 'varchar', nullable: false, unique: true }) username: string;
  @Column({ type: 'varchar', nullable: false }) password: string;

  @OneToMany(
    type => Post,
    post => post.user,
    {
      eager: true,
    },
  )
  posts: Post[];
  @OneToMany(
    type => Comment,
    comment => comment.user,
  )
  comments: Comment[];

  constructor(name?: string, posts?: []);
  constructor(name?: string) {
    this.name = name || '';
  }

  @BeforeInsert() async hashPassword() {
    this.password = await bcrypt.hash(this.password, 10);
    this.uid = uuid();
  }
}

评论

/**
 * Comments Entity
 */
@Entity('comments')
export class Comment {
  @PrimaryGeneratedColumn('uuid') comment_id: string;
  @Column('text') content: string;
  @ManyToOne(
    type => Post,
    post => post.comments,
  )
  post: Post;
  @ManyToOne(
    type => User,
    user => user.comments,
  )
  user: User;
}

为什么会这样? 为什么没有指定where 子句?

TypeORM 版本:^0.2.22 打字稿:^3.7.4

【问题讨论】:

    标签: postgresql uuid nestjs typeorm


    【解决方案1】:

    显然这些部分的库什很有效,我没有阅读自己的代码..

    post.controller.ts 中,@Get() 装饰器缺少 find 关键字,我的请求如下所示:

    http://localhost:3000/posts/find 其中find 没有在控制器中定义为路由

    解决方案是从nestjs/common 添加@Get('find')

    Post.controller.ts

      /**
       * Get all posts from all users
       */
      @Get('find')
      @ApiCreatedResponse({
        status: 201,
        description: 'All posts have been successfully retreived.',
        type: [PostDTO],
      })
      @ApiResponse({ status: 403, description: 'Forbidden.' })
      async find() {
        try {
          return this.postService.findAll();
        } catch (error) {
          throw new Error(error);
        }
      }
    
    

    Post.service.ts

     /**
       * Find all posts 
       */
      async findAll(): Promise<Post[]> {
        const posts = await this.postRepository.find();
        return posts;
      }
    

    附言。我将添加nestjs标签,因为这实际上与它的关系比TypeORM或PG更多

    【讨论】:

    • 我遇到了类似的错误,并不断回到这个问题上。最终发现(通过github.com/nestjs/nest/issues/1667)列出路线的顺序很重要。遗憾的是,Controllers 文档中没有关于此的建议。在我的具体情况下,我必须将我的 @Get() 呼叫分组,而不是在 @Post@Patch@Delete 之后列出它们。
    猜你喜欢
    • 2021-11-03
    • 2021-11-04
    • 2018-05-22
    • 2021-05-06
    • 2019-03-09
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    相关资源
    最近更新 更多