【问题标题】:Vuex Mutation: Cannot read properties of undefined (reading 'Id')Vuex Mutation:无法读取未定义的属性(读取“Id”)
【发布时间】:2022-01-14 12:55:28
【问题描述】:

我似乎遇到了一个奇怪的问题。我有一个突变,它在我的状态下从一个数组中删除一个对象,然后禁用另一个数组中的同一个对象。大多数时候它工作正常,我添加它们并用我的按钮删除它们。但是在我完成了 4 到 5 次之后(似乎是随机的次数),下次我点击它时会出现错误:

500
Cannot read properties of undefined (reading 'Id')

控制台:

TypeError: Cannot read properties of undefined (reading 'Id')
at eval (mutations.js?014a:69:1)
    at Array.find (<anonymous>)
    at Store.BOOKING_REMOVE_SESSION (mutations.js?014a:68:1)
    at wrappedMutationHandler (vuex.esm.js?2f62:844:1)
    at commitIterator (vuex.esm.js?2f62:466:1)
    at Array.forEach (<anonymous>)
    at eval (vuex.esm.js?2f62:465:1)
    at Store._withCommit (vuex.esm.js?2f62:624:1)
    at Store.commit (vuex.esm.js?2f62:464:1)
    at Store.boundCommit [as commit] (vuex.esm.js?2f62:409:1)

我的突变:

BOOKING_REMOVE_SESSION: (state, sessionData) => {
  state.performBooking.sessions.find((o,i) => {
    if(o.Id === sessionData.Id){
      state.performBooking.sessions.splice(i, 1)
    } 
  })

  let sessionIndex = state.currentShow.sessions.findIndex(x => x.Id === sessionData.Id)
  state.currentShow.sessions[sessionIndex].activated = false;

}

它似乎完全符合我的要求,除非它出错。我认为这与 state.performBooking 数组中有一些东西有关。我认为问题在于 o.Id === sessionData.Id 由于某种原因无法找到匹配的 ID。

与调用方法的按钮配合使用:

  removeSession(sessionData) {
      this.$store.commit('BOOKING_REMOVE_SESSION', sessionData)
    }

一个样本数据是:

    {
      attributes: [Object],
      Id: 'a1g960010000sJSAAY',
      Capacity__c: 5,
      Performance__c: 'a1f960001000HMZAA2',
      Name: 'Test Performance #041',
      Time__c: '2021-11-19T15:00:00.000+0000',
      Version__c: 'General'
    },

任何帮助将不胜感激。

【问题讨论】:

    标签: vue.js nuxt.js vuex


    【解决方案1】:

    我自己回答了,让它不那么冗长。

    因为我只是在寻找 Id,所以我在变量中进行了查找。

    旧:

      state.performBooking.sessions.find((o,i) => {
        if(o.Id === sessionData.Id){
          state.performBooking.sessions.splice(i, 1)
        } 
      })
    

    新:

    let localSessionIndex = state.performBooking.sessions.findIndex(o => o.Id === sessionData.Id)
    state.performBooking.sessions.splice(localSessionIndex, 1)
    

    【讨论】:

      猜你喜欢
      • 2018-04-21
      • 1970-01-01
      • 2021-07-07
      • 2020-04-06
      • 1970-01-01
      • 2019-11-18
      • 2020-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多