你好,我在使用依赖注入的类型库中遇到了一个问题,当我找不到一封电子邮件时一切正常,但是当该电子邮件已经存在时,它不会返回我的错误:
if (user) {
console.log('if');
return { message: 'user already exists' };
}
我在graphql上收到此错误:
“ message”:“无法为非空字段User.id返回null。”,
我的服务:
@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;
}