Search code examples
typescriptnestjstypeorm

Have to save a record in NestJs using TypeORM


I have started to learn a NestJs and I need a little bit of help In this case, I try to create a new record in the DB and for it, I use another logic other than documentation In this case, I don't have any errors and the record in DB also save But in DB the record has fields equal to null This is my code

async create(userDto: UserDto) {
    try {
      const user = new User();
      await this.userRepository.save(user, {
        data: {
          firstName: userDto.firstName,
          lastName: userDto.lastName,
          email: userDto.email,
          phone: userDto.phone,
          password: userDto.password,
        },
        reload: true,
      });
      return {
        user: user,
        message: 'The new record created successfully',
        statusCode: HttpStatus.CREATED,
      };
    } catch (err) {
      throw err;
    }
  }

Solution

  • Can you try this

    const user = this.userRepository.create({
              firstName: userDto.firstName,
              lastName: userDto.lastName,
              email: userDto.email,
              phone: userDto.phone,
              password: userDto.password,
            });
    
    await this.userRepository.save(user,{reload: true})