Anyone?
# orm-help
i
Anyone?
👀 1
m
@Italo Gama Can you share your TS code? Do you have strict mode configured in TS? Where is the "spouse_id" declaration/assignment - that's going to be where the useful information is.
n
I’m not sure from looking at the error but are you using usersUncheckedCreateInput? I would suggest not using that as it doesn’t keep the type safety instead use UserCreateInput shape.
i
Yes for sure, lets go.
export class CreateUserInput {
@IsString() @IsNotEmpty({ message: ‘Campo Nome não pode estar vazio.’ }) // required @Field(() =>
_String_)
first_name:
_string_;
@IsString() @IsNotEmpty({ message: ‘Campo Sobrenome não pode estar vazio.’ }) @Field(() =>
_String_)
last_name:
_string_;
@IsString() @IsNotEmpty({ message: ‘Campo Username não pode estar vazio.’ }) @Field(() =>
_String_)
username:
_string_;
@IsString() @IsNotEmpty({ message: ‘Campo Senha não pode estar vazio.’ }) // required @Field(() =>
_String_)
password:
_string_;
@IsDate() @Field(() =>
_Date_)
birth_date:
_Date_;
@IsString() @Field(() =>
_String_)
phone:
_string_;
@IsInt() @IsNotEmpty({ message: ‘Campo Senha não pode estar vazio.’ }) // required @Field(() =>
_Int_)
active:
_number_;
@IsEnum(
_gender_type_)
@IsNotEmpty({ message: ‘Campo Senha não pode estar vazio.’ }) // required @Field(() =>
_gender_type_)
gender:
_gender_type_;
@IsString() @IsNotEmpty({ message: ‘Campo CPF não pode estar vazio.’ }) // required @Field(() =>
_String_)
cpf:
_string_;
@IsString() @IsNotEmpty({ message: ‘Campo CPF não pode estar vazio.’ }) // required @Field(() =>
_String_)
rg:
_string_;
@IsDate() @IsNotEmpty() @Field(() =>
_Date_)
created_at:
_Date_;
@IsDate() @IsNotEmpty() @Field(() =>
_Date_)
updated_at:
_Date_;
@IsString() @Field(() =>
_String_)
photo:
_string_;
@IsEnum(
_scholarity_type_)
@IsNotEmpty({ message: ‘Campo Senha não pode estar vazio.’ }) // required @Field(() =>
_scholarity_type_)
scholarity:
_scholarity_type_;
@IsString() @Field(() =>
_String_)
academic_formation:
_string_;
@IsString() @Field(() =>
_String_)
profession:
_string_;
@IsString() @Field(() =>
_String_)
company_work:
_string_;
@IsBoolean() @Field(() =>
_Boolean_)
baptized:
_boolean_;
@IsDate() @Field(() =>
_Date_)
baptism_date:
_Date_;
@IsDate() @Field(() =>
_Date_)
marriage_date:
_Date_;
@IsDate() @Field(() =>
_Date_)
member_since:
_Date_;
@IsEnum(
_reason_entry_type_)
@IsNotEmpty({ message: ‘Campo Senha não pode estar vazio.’ }) // required @Field(() =>
_reason_entry_type_)
reason_entry:
_reason_entry_type_;
@IsString() @Field(() =>
_String_)
former_religion:
_string_;
@IsString() @Field(() =>
_String_)
belonged_church:
_string_;
@IsString() @Field(() =>
_String_)
natural_from:
_string_;
@IsString() @Field(() =>
_String_)
nationality:
_string_;
@IsString() @Field(() =>
_String_)
ministry:
_string_;
@IsBoolean() @Field(() =>
_Boolean_)
cursillo:
_boolean_;
@IsDate() @Field(() =>
_Date_)
cursillo_date:
_Date_;
@IsString() @Field(() =>
_String_)
cursillo_leaders:
_string_;
@IsBoolean() @Field(() =>
_Boolean_)
ecc:
_boolean_;
@IsDate() @Field(() =>
_Date_)
ecc_date:
_Date_;
@IsString() @Field(() =>
_String_)
ecc_leaders:
_string_;
@IsBoolean() @Field(() =>
_Boolean_)
ywc:
_boolean_;
@IsDate() @Field(() =>
_Date_)
ywc_date:
_Date_;
@IsString() @Field(() =>
_String_)
ywc_leaders:
_string_;
@IsNumber() @Field(() =>
_Int_)
@IsEmpty() spouse_id?:
_number_;
@Field(
_type_ => CreateAddressInput)
address?: CreateAddressInput; }
This is my service:
async create(_data_: CreateUserInput): _Promise_<UsersOutput> {
//look if user already exist... const userExist = await
_this_.findByEmail(_data_.username);
if (userExist) { throw
*new* HttpException('User already exist.', _HttpStatus_.BAD_REQUEST);
} //look if cpf already exist... const cpfExist = await
_this_.findByCPF(_data_.cpf);
if (cpfExist) { throw
*new* HttpException('CPF already registered.', _HttpStatus_.BAD_REQUEST);
} try { const hashedPass = hashPasswordTransform.to(
_data_.password);
const response = await
_this_.prisma.users.create({
data: { username:
_data_.username,
first_name:
_data_.first_name,
last_name:
_data_.last_name,
phone:
_data_.phone,
password: hashedPass, birth_date:
_data_.birth_date,
active:
_data_.active,
gender:
_data_.gender,
cpf:
_data_.cpf,
rg:
_data_.rg,
photo:
_data_.photo,
scholarity:
_data_.scholarity,
academic_formation:
_data_.academic_formation,
profession:
_data_.profession,
company_work:
_data_.company_work,
baptized:
_data_.baptized,
baptism_date:
_data_.baptism_date,
marriage_date:
_data_.marriage_date,
member_since:
_data_.member_since,
reason_entry:
_data_.reason_entry,
former_religion:
_data_.former_religion,
belonged_church:
_data_.belonged_church,
natural_from:
_data_.natural_from,
nationality:
_data_.nationality,
ministry:
_data_.ministry,
cursillo:
_data_.cursillo,
cursillo_date:
_data_.cursillo_date,
cursillo_leaders:
_data_.cursillo_leaders,
ecc:
_data_.ecc,
ecc_date:
_data_.ecc_date,
ecc_leaders:
_data_.ecc_leaders,
ywc:
_data_.ywc,
ywc_date:
_data_.ywc_date,
ywc_leaders:
_data_.ywc_leaders,
created_at:
_data_.created_at,
updated_at:
_data_.updated_at,
spouse_id: 1, address: { create: { street :
_data_.address.street,
number:
_data_.address.number,
neighborhood:
_data_.address.neighborhood,
city:
_data_.address.city,
zipcode:
_data_.address.zipcode,
complement:
_data_.address.complement,
state:
_data_.address.state,
created_at:
_data_.address.created_at,
updated_at:
_data_.address.updated_at
}, }, }, }); if (response) { return { users: response }; } return { error: { code:
_ErrorsTypes_.USER_NOT_CREATED,
message: ‘Ops, User was not created.’ } }; } catch (error) { throw
*new* HttpException('Error creating User', _HttpStatus_.BAD_REQUEST);
} }
I’m not using usersUncheckedCreateInput because i created my own “input” called CreateUserInput, and inside i declare @Field(type => CreateAddressInput) address?: CreateAddressInput; i need create the address with the user
m
Copy code
async create(data: CreateUserInput): Promise<UsersOutput> {
    //look if user already exist...
    const userExist = await this.findByEmail(data.username);
    if (userExist) {
      throw new HttpException('User already exist.', HttpStatus.BAD_REQUEST);
    }
    //look if cpf already exist...
    const cpfExist = await this.findByCPF(data.cpf);
    if (cpfExist) {
      throw new HttpException('CPF already registered.', HttpStatus.BAD_REQUEST);
    }
    try {
      const hashedPass = <http://hashPasswordTransform.to|hashPasswordTransform.to>(data.password);
      const response = await this.prisma.users.create({
        data: {
          username: data.username,
          first_name: data.first_name,
          last_name: data.last_name,
          phone: data.phone,
          password: hashedPass,
          birth_date: data.birth_date,
          active: data.active,
          gender: data.gender,
          cpf: data.cpf,
          rg: data.rg,
          photo: data.photo,
          scholarity: data.scholarity,
          academic_formation: data.academic_formation,
          profession: data.profession,
          company_work: data.company_work,
          baptized: data.baptized,
          baptism_date: data.baptism_date,
          marriage_date: data.marriage_date,
          member_since: data.member_since,
          reason_entry: data.reason_entry,
          former_religion: data.former_religion,
          belonged_church: data.belonged_church,
          natural_from: data.natural_from,
          nationality: data.nationality,
          ministry: data.ministry,
          cursillo: data.cursillo,
          cursillo_date: data.cursillo_date,
          cursillo_leaders: data.cursillo_leaders,
          ecc: data.ecc,
          ecc_date: data.ecc_date,
          ecc_leaders: data.ecc_leaders,
          ywc: data.ywc,
          ywc_date: data.ywc_date,
          ywc_leaders: data.ywc_leaders,
          created_at: data.created_at,
          updated_at: data.updated_at,
          spouse_id: 1,
          address: {
            create: {
              street : data.address.street,
              number: data.address.number,
              neighborhood: data.address.neighborhood,
              city: data.address.city,
              zipcode: data.address.zipcode,
              complement: data.address.complement,
              state: data.address.state,
              created_at: data.address.created_at,
              updated_at: data.address.updated_at
            },
          },
        },
      });
      if (response) {
        return {
          users: response
        };
      }
      return {
        error: {
          code: ErrorsTypes.USER_NOT_CREATED,
          message: 'Ops, User was not created.'
        }
      };
    } catch (error) {
      throw new HttpException('Error creating User', HttpStatus.BAD_REQUEST);
    }
  }
Oh - and you're trying to send in a hard-coded spouse_id and it isn't working??
i
the spouse_id is a field that references a user itself on the table. On the example i tried to sed hard-coded, just like i tried to pass with “data.spouse.id” it doesnt work
i get the error “type number is not assignable to type ‘never’ ”
m
If I were you, I think I'd try to create the object that you're trying to use in
data
, and I would try to explicitly type it to what you want it to be. That Without<...> seems weird. I can't be certain, but I'd guess it's not supposed to include spouse_id and that's why you're seeing that error.
i
i can’t do this. the object “spouse_id” it’s a ID from an user you know? spouse is the wife or fiance, so i need to put one ID from one user, that is the fiance(or wife) of that user
m
@Italo Gama Okay. This actually just happened to me too. Which is weird, because I've definitely not had problems with this preivously. Here's how I solved it - maybe something similar will work for you. In your create statement, try something like this:
spouse_id: typeof data.spouse_id === "number" ? spouse_id : 0
This is a general idea, not the exact solution to your problem. Hope it helps.
Basically the point I'm trying to make is that TypeSript needs you to whittle down the possible type of spouse_id. For instance, I'm getting the same error on an email field which I know will be a string. But TypeSript doesn't know it will be a string. In my case, my best bet is gating the input like so:
Copy code
if (typeof newEmail.email !== "string") throw new InternalServerErrorException()
Then TypeScript will KNOW that spouse_id is of the type you think it is.
i
i’ll try!
It didnt work 😞
a
Could you share your actual
schema.prisma
?