0

In my Vuex store i have a action which once get result, loop over each element of the result and add's new property to each one.

Vue.axios
  .get(endpoint, headers)
  .then(res => {
    if (Array.isArray(res.data) && res.data.length) {
      res.data.forEach(el => {
          el.link = 'some_dynamic_link'
      })
      commit('SET_ACTIVITY', res.data)
    }
  })

the problem is that my store ends up with the data from rsult, WITHOUT the link added to any of the elements. i thought that something like this tweak will help:
if (_.has(res.data[res.data.length - 1], 'link')) { commit('SET_ACTIVITY', res.data) } but then the mutation just ends up not executing at all, any idea how can i achieve this synchronous behavior ?

update tried to use map instead:

.then(res => {
  if (Array.isArray(res.data) && res.data.length) {
    commit('SET_ACTIVITY', res.data.map(el => {
        el.link = 'some_dynamic_link'
    }))
  }
})

but than the store contains activities array where elements are undefined's

7
  • Use map instead of forEach. Check this answer Commented Jan 20, 2019 at 18:32
  • assigning map to a variable and passing it to the commit ends up with the store holding array of undefined Commented Jan 20, 2019 at 18:50
  • Can you add map example to the question? Commented Jan 20, 2019 at 18:52
  • updated my question with the example Commented Jan 20, 2019 at 19:01
  • 1
    You are missing return el in your map. Each iteration of map MUST return something that will go into final array Commented Jan 20, 2019 at 19:03

0

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.