Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJS: Uncaught (in promise) TypeError: Cannot read property 'push' of undefined

I am getting the 'cannot read property push of undefined' error: here is my vueJs code:

data:{
CoIsignedListUID:[]
}
methods:{

fetchCoISigned: function () {
            this.$http.get('/cimsm/public/api/fetchCoIsigned/' + this.conflictofInterest.complaintID).then(function (response) {
                var data = response.data;
                this.$set('CoIsignedList', data);
                data.forEach(function (detail) {
                    this.CoIsignedListUID.push(detail.uID);
                });
            });

what am i doing wrong? Thanks

like image 921
Tashi Tobgay Avatar asked Jul 19 '16 13:07

Tashi Tobgay


1 Answers

this.CoIsignedListUID is not defined

probably because this is not the this you think it is

you should do

var _this = this

outside the function and then

_this.CoIsignedListUID.push(detail.uID);

Alternatively, you can use ES2015 arrow syntax.

Instead of:

.then(function (response) {}

Use:

.then((response) => {}

The 'this' is now available inside the function so no need for creating a new variable. Full details Here.

like image 84
gurghet Avatar answered Sep 21 '22 05:09

gurghet