Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js gives weird object with Observer data

I'm receiving data with axios like this:

getData() {                                        
    Axios.get(                                        
        '/vue/get-data/',                           
        {                                             
            params: {                                 
                categories: this.category,            
                activeFilters: this.activeFilters,    
            }                                         
        }                                             
    ).then((response) => {                            
        this.banners = response.data;                 
        this.setBanner();                             
    })                                                
},           

Then I get this:

enter image description here

When I try console.log(response.data.length) I get undefined. What could be going on here very weird!

When I look in my vue-devtools banners has 2 objects:

enter image description here

So how can response.data.length be undefined?

like image 231
Jenssen Avatar asked Dec 08 '17 09:12

Jenssen


People also ask

What does $t mean in Vue?

Vuex is a state management pattern for vue. js. $t is the injected method from vue. js or Vue. i18n.

Is data in Vue reactive?

One of Vue's most distinctive features is the unobtrusive reactivity system. Component state consists of reactive JavaScript objects. When you modify them, the view updates. It makes state management simple and intuitive, but it's also important to understand how it works to avoid some common gotchas.

Does Vue use observables?

observable transforms an object into a reactive entity. Vue uses this internally on the object returned by the data function. When changed, the resulting object can be directly utilized inside render functions and calculated properties, and it will trigger necessary modifications.

What is Observer object in JS?

The Observer pattern works by one object — the observer — subscribing to another object — the subject — by way of a callback method. Whenever an event occurs on the subject, it notifies the observer that something happened.


1 Answers

You are getting object not array that why .length is not working, and you are getting as undefined

this.banners = response.data[0];// for first 

Or loop over this, to get each object's data

for(var i in response.data){
     console.log(response.data[i]);
}

If to get each value is not your purpose , and you want to just size check this answer

like image 58
freelancer Avatar answered Oct 12 '22 14:10

freelancer