0

I am having problems in an html tag that uses an object from my object in store.

When i refresh my page, the array from my sotre is empty, so i when i refresh in the index page, it will first load the html, then the mounted method, and its where i fill my store. its says that Cannot read property 'substring' of undefined

index.vue that i use this object:

          <p v-html="pegaPrimeiroPost.conteudo.substring(0,500)"></p>

export default of index.vue:

  computed: {
    ...mapGetters({
      postsDB: "postagensDB/pegaPosts",
      pegaPrimeiroPost: "postagensDB/pegaPrimeiroPost",
    }),
  },
  methods: {
    ...mapActions({
      buscaPostDB: "postagensDB/pegaPostsDB",
    }),
  },
  async mounted() {
    **await this.buscaPostDB();**
    });
  },

this object pegaPrimeiroPost is an objetct from my array that i fill from my database.

store/postagensDB:

import axios from 'axios'

export const state = () => ({
    posts: [],
    primeiroPost: {},
})

export const getters = {
    pegaPosts(state) {
        return state.posts;
    },
    pegaPrimeiroPost(state) {
        return state.primeiroPost;
    },

}

export const actions = {
    async pegaPostsDB(state) {
        await axios
            .get("http:/MY_API_ADRESS")
            .then((response) => {
                state.commit('carregaStatePosts', response.data)
            })
            .catch((response) => {
                console.log(response)
            });
    },

}

export const mutations = {
   async carregaStatePosts(state, postsDB) {
        state.posts = postsDB.posts;
        state.primeiroPost = state.posts[0];
    },
}

If i erase the substring() method, reload the page, then it will fill my store; and then re-add the substring(), it works, but wont solve my prob. Can anyone help me?

1 Answer 1

1

I've never used Vue, but this sounds like you have some sort of async action that populates the object, but your default value (used before the async call has completed) doesn't have that property. The quickest solution is probably just give it a value if it doesn't exist:

(pegaPrimeiroPost.conteudo || '').substring(0,500)

or set a "default" object with that property:

export const state = () => ({
    posts: [],
    primeiroPost: { conteudo: '' },
})
Sign up to request clarification or add additional context in comments.

2 Comments

The first solution works PERFECT!!!! I have been on this problem for hours and had no idea that i could make an IF inside of this v-html prob. Honestly, i have never seen this. What is the name of this type of comparation? If someting is not what i want, then use ' ' or something else?
It's just a logical or operator - which returns the right-hand side operand if the left operand is any falsy value. You might even be better off using the nullish coalescing operator ??, but in this case (a string) it's probably equivalent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.