I want to push an object into an array inside a data attribute in my nuxt project. But I am getting error like
Cannot read property of undefined (read 'push')
This 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>
P粉5931184252024-02-27 11:05:43
I tried your code like above. When I click the button it is working. You may not be able to access form.gallery because the uploadGallery triggered is in the wrong lifecycle hook.
{{form.gallery}}