Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

276
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!