Im exploring vue and new javascript tools - things are a bit convoluted for me, in the sense I struggle to debug and understand what is going on.
I am trying to fetch data via a simple API, using axios as suggested here: https://v2.vuejs.org/v2/cookbook/using-axios-to-consume-apis.html#Real-World-Example-Working-with-the-Data
turns out, if I use "normal" javascript, data will not be set.
It must have something to do with interpreting "this" element.
Instead, if I use arrows functions, data are set.
data () {
  return {
     query : {
        info : null
     }          
}
methods : {
    
   test : function(query) {
             params = {
               origin : '*',
               action : 'query',   
               prop : 'pageimages',
               pithumbsize : 400,
               pilimit : 'max',
               list : 'search',
               srsearch : query,
               utf8 : true,
               format : 'json'
              }
              axios.get('https://en.wikipedia.org/w/api.php', {
                params : params 
              })
              .then(function (response) {
                
                // won't work :     this.query.info // undefined        
                this.query.info = response.data.query.search
               
              })
              .then( 
               // this.query.info is set
               response => {
                        this.query.info = response.data.query.search
                      }
              )
  
   }
} 
Can you explain why?
I helped myself with answer in : What does "this" refer to in arrow functions in ES6? - "this refers to the enclosing context" :
- what does - thisrefer to in the pattern- then(function(response){ this.[...])and in the patter- then( response => this.[...])?
- how could I use syntax without arrows functions, and use - thisreferring to the- data()model of vue ?
Would you mind to also suggest working practices to keep code clean and simple?
I came from using native javascript and jquery, and despite a bit more verbose, I could understand well what was going on and easily debug.
Instead with these new tools, despite powerful, I am a bit lost for it seems there is an overload of other tools to use to get things done.
While learning I still would prefer to code a bit more verbosely and use web console to debug, instead of nodes plugin and syntax that must be compiled by frameworks.
__
Edited
Upon comments, I tried:
            .then(function(response){
            // won't work : here thisArg should be the vue app,
            // so self.query.info should bind to 
            // [App data()].query.info 
            // still, query.info is undefined
            // though response was successfully fetched
            var self = this
            self.query.info = response.data.query.search
          })
Edited 2 (Answer)
I found this answer helpful: https://stackoverflow.com/a/29881419/305883
so I realised above patter was wrong, for writing var self =this within the then() means I am still referenceing the this of the promise object, which is undefined.
so with "normal" functions, I should write something like:
var self = this; // now self is referenced to the external object, the Vue app
              fetchResponseFromApi(query)
              .then(function(response){
                
                self.query.info = response.data.query.search
              }) 
This answer address my question (no pun for "this" answer, hohoho :).
In other answers I read comments not to pass around the thisArg, like into encapsulated functions:
comments about clarifying about good coding patterns will be of good for others to.
 
    