O
O
ObehanProger2019-08-14 10:18:02
Vue.js
ObehanProger, 2019-08-14 10:18:02

Why is the length of the object received from storage not determined in Vuex?

I'm using Vuex storage to filter news by author. Here are the main elements:

Vue.use(Vuex)
  const store = new Vuex.Store({
    state: {
        news: null,
      author_id:'all'
      },
    getters: {
      NEWS: state => {
        return state.news;
      },
      AUTHOR: state => {
        return state.author_id;
      }
    },
    mutations: {
      SET_NEWS: (state, payload) => {
          state.news = payload;
      },
      SET_AUTHOR: (state, payload) => {
          state.author_id = payload;
      }
    },
    actions: {
      GET_NEWS: async (context, author_id='all') => {
        let data = await axios.get('http://news/author_'+author_id).then(response => {return response.data});
              context.commit('SET_NEWS', data);
      },
      GET_AUTHOR: async (context,author_id) => {
              context.commit('SET_AUTHOR', author_id);
      }
    }
  });
  const app = new Vue({
    el: '#content',
    store,
    data: {
        selected_author: 'all'
    },
    mounted() {
      this.$store.dispatch('GET_NEWS','all');
    },
    methods:{
      async onGetNews () {
      			await this.$store.dispatch('GET_NEWS',this.selected_author);
      			console.log(this.$store.getters.NEWS.length);
      		}
    }
  });

When calling the onGetNews method in the console, it says: Error in render: "RangeError: Invalid array length", and the array length is undefined.
If called in onGetNews(): then it displays the object:
console.log(this.$store.getters.NEWS)
{__ob__: Observer}
12: (...)
13: (...)
__ob__: Observer {value: {…}, dep: Dep, vmCount: 0}
get 12: ƒ reactiveGetter()set 12: ƒ reactiveSetter(newVal)
get 13: ƒ reactiveGetter()set 13: ƒ reactiveSetter(newVal)
__proto__: Object

Why does not determine the length of this object? How to determine?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question