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

278
Vistas
Can't assign string gotten from reader.result to variable, returns null

I have the following code

It returns a string when in the reader.onload function but null when i console.log image.Url in the outer function

EDIT: This is the full code


export default {
  data() {
    return {
      item: {
        image: null
      },
      imageUrl: null,
    };
  },

  methods: {,
    uploadImage(e) {
      let image = e.target.files[0];
      const reader = new FileReader();
      reader.readAsDataURL(image);

      reader.onload = function(){
        this.imageUrl = reader.result
        console.log(this.imageUrl)
        // this.setImageOne(this.imageUrl)
      }
      console.log(this.imageUrl)
      

    },

EDIT:

Fixed it. Thanks everyone

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Things aren't happening in the order you expect.

This is the order they are happening:

  1. uploadImage() called
  2. the second console.log runs and imageUrl is null (the callback function is registered for the onload event)
  3. the onload callback is run and imageUrl is set
 function(){
        this.imageUrl = reader.result
        console.log(this.imageUrl)
        // this.setImageOne(this.imageUrl)
      }

This function is not run when it is defined. Js only runs the callback function you provide when the onload event happens. It registers this callback and then continues on to the console.log.

If you want to use the value of imageUrl you should do that in the callback function.

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