Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

185
Visualizações
Adding username from signup to populate a datafield from other collection using angular

I am trying to add the username from the user who signed to populate a data field of a form. I got the username however it isnt being placed in the variable to be posted to the database. The function is as follows:

    addMessage() {
        this.mocsignup.getUserProfile().subscribe((res) => {
          this.mocUserDetails = res['user'];
        });
        let userDetail = this.mocUserDetails.username;
        console.log(userDetail);
        let formData: any = {
          username: this.form.value.userDetail,
          message: this.form.value.message,
        }
        this.messageService.postMessage(formData).subscribe((d) => {
          console.log(d);
        });
        //window.location.reload();
      }

This piece of code gets the username and saves it to the userDetail variable that is verified by the console.log results:

    this.mocsignup.getUserProfile().subscribe((res) => {
              this.mocUserDetails = res['user'];
            });
            let userDetail = this.mocUserDetails.username;
            console.log(userDetail);
console.log results:

enter image description here

This is the intended results.

however when trying to post to the database using the following code:

    let formData: any = {
              username: this.form.value.userDetail,
              message: this.form.value.message,
            }
            this.messageService.postMessage(formData).subscribe((d) => {
              console.log(d);
            });

It does not pust to the username in the collection, instead it skips it and sends the message and messageDateTime:

enter image description here

It is supposed to have a username field stored in the results above doesnt. Please help!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Note that you have 2 different userDetail variables:

  1. One that you created with let userDetail = this.mocUserDetails.username;
  2. One in a form this.form.value.userDetail

You are storing data in the first one, but later you are referencing the second one in the formData. Try changing your code like this:

let formData: any = {
  username: userDetail ,
  message: this.form.value.message,
}
about 4 years ago · Juan Pablo Isaza Relatório

0

If you're getUserDetails()is doing a api call then that method would be an asynchronous one, if that's the case the the code won't be waiting for the api to give back the userName and it'll skip to next block of statements. So if you want to use the results of getUserDetails() then move the statements within the method's block, like this

this.mocsignup.getUserProfile().subscribe((res) => {
              this.mocUserDetails = res['user'];
            let userDetail = this.mocUserDetails.username;
            console.log(userDetail);
            });
            
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda