解析器返回时带有typeorm错误的打字稿

时间:2020-06-06 23:08:07

标签: typescript graphql typeorm

你好,我在使用依赖注入的类型库中遇到了一个问题,当我找不到一封电子邮件时一切正常,但是当该电子邮件已经存在时,它不会返回我的错误:

if (user) {
  console.log('if');
  return { message: 'user already exists' };
}

我在graphql上收到此错误:

“ message”:“无法为非空字段User.id返回null。”,

///// enter image description here ////

我的服务:

@Service()
export class UserService {
  constructor(
    @InjectRepository(User)
    private userRep: Repository<User>,
  ) {}

  async findOrCreate(data: Partial<User>): Promise<Partial<User> | Object> {
    let user = await this.userRep.findOne({ where: { email: data.email } });
    if (user) {
      console.log('if');
      return { message: 'user already exists' };
    }
    console.log('without if');
    data.password = await bcrypt.hash(data.password, 12);
    user = await this.userRep.save({
      ...data,
    });
    return user;
  }
}

我的解析器:

  //create User Mutaton
  @Mutation(() => User)
  async register(
    @Arg('data')
    data: RegisterInput,
  ): Promise<User | Object> {
    const user = this.userService.findOrCreate(data);
    console.log(user);
    return user;
  }

0 个答案:

没有答案