【问题标题】:Vuex commit : JSON circular structure errorVuex提交:JSON循环结构错误
【发布时间】:2019-05-03 21:38:11
【问题描述】:

我在 vuex 商店中使用 Vue.js。我调用一个 API 方法来验证一个项目,该方法返回一个错误数组和一个警告数组。

我的 vuex 操作:

export function validateitemReview ({ commit, dispatch, state }, { reviewId, type, itemreviewData }) {
  console.log('*** validateItemReview() called')
  return api.post(`/clients/districts/reviews/${reviewId}/${type}/validate`, itemreviewData).then(response => {
    console.log('response.data :')
    console.log(response.data)
    commit('setItemReviewsErrors', 'teststring')
  })
}

如您所见,我还没有对响应做太多事情。 vuex store 中被调用的setItemReviewsErrors 变异:

export const setItemReviewsErrors = (state, data) => {
  console.log('*** setItemReviewsErrors() called, data:')
  console.log(data)
}

我的问题来了,我的控制台输出如下:

*** validateItemReview() called
response.data :
{
    fatal_errors: []
    warnings: []
    __proto__: Object
}
*** setItemReviewsErrors() called, data:
teststring

直接跟这个错误:

Uncaught (in promise) TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)
    at eval (vuex-persistedstate.es.js?0e44:1)
    at eval (vuex-persistedstate.es.js?0e44:1)
    at eval (vuex.esm.js?2f62:392)
    at Array.forEach (<anonymous>)
    at Store.commit (vuex.esm.js?2f62:392)
    at Store.boundCommit [as commit] (vuex.esm.js?2f62:335)
    at local.commit (vuex.esm.js?2f62:651)
    at eval (itemreview_actions.js?0d87:62)

itemreview_actions.js?0d87:62 是我的 vuex validateitemReview() 操作中的以下行:

commit('setItemReviewsErrors', 'teststring')

如果我评论它,就不会再有错误了。当问题似乎来自提交一个简单的字符串时,我无法确定我的“循环结构”在哪里。

更好,为什么:

  • 我的来自setItemReviewsErrors 突变的console.log() 仍会打印出来,即使错误似乎是在调用此方法时出现的错误
  • 如果我重新加载页面(在浏览器中按 Ctrl+R),则不会出现错误,但如果我离开它(转到另一个页面)然后返回,则会再次引发错误。

更新

问题似乎来自插件 vuex-persistedstate 。我发现这个应用程序的 vuex 商店正在使用它。我并不孤单:

但开发人员只是关闭了票证。如果有人能解决这个问题,我不允许删除持久性插件。

【问题讨论】:

  • 使用支持循环 json 的解析器。 github.com/WebReflection/flatted
  • 这可能是一种解决方法,但 JSON 没有理由认为这里存在循环结构,所以我想先解决这个问题
  • a JSON 是一个字符串。它如何包含循环引用?
  • @Tom 我在使用这个插件时遇到了完全相同的问题 (github.com/robinvdvleuten/vuex-persistedstate/issues/…) 我的代码在没有这个插件的情况下运行良好,但是在启用它时无法转换我的 JSON。我只是将它从我的依赖项中删除并重新修改我的代码以具有相同的行为。开发人员似乎关闭了有关此错误的所有问题,但我认为它们是插件代码中的问题。
  • 如果你愿意,我可以向你解释我是如何让它工作的。但这取决于您的需求(如果您有与我相同的需求),但与此问题无关。

标签: javascript vue.js vuejs2 vuex


【解决方案1】:

查看这个备用库,vuex-persist,他们正面遇到了这个问题

如果您所在的州有圆形结构

let x = { a: 10 }  
x.x = x  
x.x === x.x.x // true  
x.x.x.a === x.x.x.x.a //true  

JSON.parse() 和 JSON.stringify() 将不起作用。

你需要安装circum-json

npm install circular-json

并且在构建 store 的时候,添加 supportCircular 标志

new VuexPersistence({
  supportCircular: true,
  ...
})

但是,如果在某个阶段,反应性属性触发了对同一突变的另一个调用,则状态中的循环引用可能会导致其他问题。也就是说,您可能会很快将问题视为超出最大调用堆栈大小错误。

查看此代码上的comment by Linus Borg

mutations:
saveVideoComment(state, {id, text, videoId}) {
    let comment = {
        id: id,
        videoId: videoId,
        text: text,
        inlineUserVideo: state.userVideos[userVideoId]
    };
    Vue.set(state.videoComments, id, comment);
    state.videos[videoId].comments.push(id);
    state.videos[videoId].inlineComments.push(comment);
}

【讨论】:

  • 即使我的老板告诉我我花了足够的时间,我找到了另一种解决方法,这似乎是一个可以接受的答案,谢谢!
  • 大声笑不客气。告诉你的老板你正在节省未来的返工!
猜你喜欢
  • 2019-01-31
  • 1970-01-01
  • 2018-09-17
  • 2018-05-05
  • 1970-01-01
  • 1970-01-01
  • 2018-06-08
  • 2021-10-23
相关资源
最近更新 更多