[SOLVED] this.$setState or self.setState is not a function

Hello community,

www/pages
www/scripts

  1. www/pages/progress-tracker.html

//my template here, then respected script as below
return {
data:function(){
return{
items:null,
loading:true
}
},
on: {
pageInit: function () {
var app = this.$app;
var $$ = Dom7;
var self = this;
var router = this.$router;
progresstracker(); // this function called from another JS file
}
}
}

  1. www/scripts/progress-tracker.js

function progresstracker(){
// my API CALLS
app.request({
url: apiUrl,
method:‘GET’,
async:false,
dataType:‘json’,
sucess:(data)=>{
data.map((newData)=>{
items.push({ // added new items successfully
})
})
// issue on line below
self.$setState({
items:items
})
}
});
}

  1. Issue:
    uncaught TypeError : self.$setState is not function

You can add the reference as a parameter. But I don’t know if this is a logical solution. It would be better to call a callback.

parameter

  function progresstracker(self){

    }

callback

 function progresstracker(callback){
app.request({
sucess:(data)=>{

callback(data);

})

}
});
  }

progresstracker((data)=>{
  console.log(data);
});
1 Like

reference as a parameter worked
Thank You :smile: