0

i'm trying to add multiple images and i can't find a way to make it work.

I have this method:

        imageAdd(e) {         
            e.forEach(function(e) {
            if (e.type == 'image/jpeg' || e.type == 'image/png')
            {
                this.images.push({
                    image: URL.createObjectURL(e),
                    imageData: e
                    })
            }
            })
   },

When i try to push to images array, it brings me this error Cannot read property 'images' of undefined although it is defined inside my data. What's the problem here?

3
  • 1
    .forEach(/*...*/, this) or .forEach(/*...*/.bind(this) or related. Commented Dec 1, 2020 at 16:07
  • @ASDFGerte or an arrow function, I guess. Commented Dec 1, 2020 at 16:07
  • True, or use for ... of instead of .forEach, which is often superior anyways Commented Dec 1, 2020 at 16:07

1 Answer 1

1

Use arrow function instead a usual one that has its own this:

 imageAdd(e) {         
            e.forEach((e) => {
            if (e.type == 'image/jpeg' || e.type == 'image/png')
            {
                this.images.push({
                    image: URL.createObjectURL(e),
                    imageData: e
                    })
            }
            })
   }
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, made it work like a charm. I used a for statement but this one looks more clean to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.