0

I created Vue application and I have simple store:

const paginationStore = {
  data: {
    entitiesDisplayOptions: [
      { value: '1', text: '1' },
      { value: '2', text: '2' },
      { value: '4', text: '4' },
      { value: '999999', text: 'All' },
    ],
    paginationLimits: {
      bottom: 0,
      top: paginationStore.data.entitiesDisplayOptions[0].value
    }
  }
}

I'm getting error:

Uncaught TypeError: Cannot read property 'data' of undefined

Why I can't set value from entitiesDisplayOptions to paginationStore.data.paginationLimits.top? What should I do to make it works?

2 Answers2

0

You could store it in a variable outside the definition:

const newEntitiesDisplayOptions = [
      { value: '1', text: '1' },
      { value: '2', text: '2' },
      { value: '4', text: '4' },
      { value: '999999', text: 'All' },
];

const paginationStore = {
  data: {
    entitiesDisplayOptions: newEntitiesDisplayOptions ,
    paginationLimits: {
      bottom: 0,
      top: newEntitiesDisplayOptions[0].value
    }
  }
}
Michael Hancock
  • 2,673
  • 1
  • 18
  • 37
0

There is lot of possibilities. Everything you have to do is to remember you can't reference the other object properties at the moment of creation of object in the form of object literal.

const entitiesDisplayOptions = [
  { value: '1', text: '1' },
  { value: '2', text: '2' },
  { value: '4', text: '4' },
  { value: '999999', text: 'All' },
]

const paginationLimits = {
  bottom: 0,
  top: paginationStore.data.entitiesDisplayOptions[0].value
}

const paginationStore = {
  data: {
    entitiesDisplayOptions,
    paginationLimits
  }
}
Vladislav Ladicky
  • 2,261
  • 1
  • 11
  • 13