Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

182
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda