Ho can I get the username of object from select list?

Viewed 50

I have select list contains names, how can I get the name! I want to insert name in database and not the id!

here is it my ts file:

  selectuser() {
    this.UtilisateurService.findAll().then((res) => {
      let id = '';
      this.listuser = res.map(function (obj: any) {
        return {
          id: obj.id,
          text: obj.username,
        };
      });
      this.listuser.splice(0, 0, {
        id: 'empty',
        text: '-'
      });
      if (id !== '') {
        this.idUser = id;
 
      }
    });
  } 

// I want to insert the value of this.nameUser in obj.username:

  nameUser:string;

 onSubmit() {
    this.submitted = true;
    if(this.messageForm.valid) {
      this.success = true;
      let obj = this.messageForm.value;
      obj.id_examen = this.idExamen;
      obj.id_coach=sessionStorage.getItem('id');
      obj.id_cible=this.idUser;
     -------> obj.username=this.nameUser;
 
        this.coachingService.addLigneExamen(obj).then(res => {
          if (res === true) {
            this.closemodal()
          }
          this.closemodal()
        });
    }
  }
1 Answers

You have list of user with their id. You need to find it in your list

obj.username = this.listuser.find(user => user.id === this.idUser).text
Related