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

181
Visualizações
How to set model value in angular

I am trying to send a model to my backend from my frontend angular, this is how I define it in the class I want to use:

  documentsArray : DocumentModel

Now I have this method:

uploadFile(files: FileList | null): void {
      if(files){
        const file = files.item(0)
        const reader = new FileReader()
        reader.readAsDataURL(file)
        reader.onload = () => {
        this.documentsArray.filename = file.name,
        this.api.saveDocument({body: 
         this.documentsArray        
        }).subscribe({
          next: data => console.log(data),
          error: error => console.log("your error: " + error)
        })
        console.log(this.documentsArray[0])
        console.log(JSON.stringify(this.documentsArray))
        }
      }
    }

But then I try to set this part:

    this.documentsArray.filename = file.name,

I am getting the following error:

TypeError: Cannot set properties of undefined (setting 'filename')

The model only has strings. Could someone tell me my error.

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

0

First: you need to clarify whehter documentsArray is really an Array.

If yes, you have to specify the index at line:

this.documentsArray.filename = file.name,

As follow:

this.documentsArray[0].filename = file.name,

Guessing you want to set the first element name

If no, please show the entire file in a online code editor, such us CodePen or StackBlitz and share the link here

about 4 years ago · Juan Pablo Isaza Relatório

0

documentsArray is undefined. Therefore you cant reach undefined.filename; you have to declare it like documentsArray = {};

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