0

I want to push object into array inside data properties in my nuxt project. but i am getting error like

Cannot read properties of undefined (reading 'push')

here is my js part

<script>
export default {
  data() {
    return {
      form: {
        gallery: [{
          id: null,
          imgurl: null
        }]
      }
    }
  },

  methods: {
    async uploadGallery() {
      var myimg = "imageurl getting after upload";
      var imgData = {};
      imgData['imgurl'] = myimg;
      imgData['id'] = "someid";
      this.form.gallery.push(imgData);
    },
  }
}
</script>
5
  • 3
    This means that this.form.gallery is undefined at some point. You probably have some async issues somewhere else in your code. Commented Jan 8, 2022 at 20:56
  • Also, feel free to write it like this imgData.imgurl, simpler. Commented Jan 8, 2022 at 20:59
  • when are you calling this method ? Commented Jan 8, 2022 at 21:21
  • pardon me ! it was all typo issue on my backend side, in asyncdata i replace local form data from using returned value from api. in backend i mistyped gallery as galley, thats why i was facing such issue. Commented Jan 9, 2022 at 11:52
  • 'this' content is available at the "fetch" hook. You are probably calling the method in created hook. More information about nuxt life cycle: nuxtjs.org/docs/concepts/nuxt-lifecycle Commented Jan 11, 2022 at 10:44

1 Answer 1

1

l tried your code like above. When l clicked the button, it was working. Probably you couldn't reach form.gallery becuse triggered uploadGallery is in wrong life cycle hooks.

<template>
  <div>
    {{form.gallery}}
     <button @click="uploadGallery">click</button>
  </div> 
</template> 
Sign up to request clarification or add additional context in comments.

Comments

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.