【问题标题】:Error: Cannot return null for non-nullable postgress错误:不能为不可为空的 postgres 返回 null
【发布时间】:2021-05-11 02:16:12
【问题描述】:
 @Query(() => Post, { nullable: true })
  async post(
    @Arg("id", () => Int) id: number
  ): Promise<Post | undefined | null> {
    return await Post.findOne({ id });
  }

我有这个运行良好并返回所需输出的查询。但是当我将其转换为 postgres sql 时,它没有显示错误。

@Query(() => Post, { nullable: true })
  async post(
    @Arg("id", () => Int) id: number
  ): Promise<Post | undefined | null> {
    
    const posts = await getConnection().query(
      `
          SELECT * FROM post where id = 2
          
        `
    );

    return posts
  }

它抛出错误Error: Cannot return null for non-nullable

我是 postgres 的新手。什么错误?

【问题讨论】:

    标签: node.js postgresql graphql typeorm


    【解决方案1】:

    问题在于,在第一个示例中,TypeORM 将返回一个帖子或 null。在第二个示例中,它将返回一个包含或不包含某些内容的数组。我建议您对查询设置一个限制

    SELECT * FROM post where id = 2 limit 1

    并返回结果数组中的第一个元素(返回帖子[0])。

    【讨论】:

      猜你喜欢
      • 2019-05-28
      • 2021-08-06
      • 2021-12-09
      • 2021-10-07
      • 2021-05-21
      • 2021-07-01
      • 2019-10-25
      • 2021-03-18
      • 2019-07-08
      相关资源
      最近更新 更多