【问题标题】:How to fix "Maximum call stack size exceeded" error如何修复“超出最大调用堆栈大小”错误
【发布时间】:2019-09-09 23:03:02
【问题描述】:

我想在渲染页面之前从 firebase 实时数据库中获取数据,我正在使用 vuex 存储来处理我的所有数据。

store/index.js

state: {
      loadedPosts: []
    },
    mutations: {
      setPosts(state, posts) {
        state.loadedPosts = posts;
      },

    },
    actions: {
      nuxtServerInit(vuexContext, context) {
        return axios
          .get("https://new-nuxt-blog.firebaseio.com/posts.json")
          .then(res => {
            const postsArray = [];
            for (const key in res.data) {
              postsArray.push({ ...res.data[key], id: key });
            }
            vuexContext.commit("setPosts", postsArray);
          })
          .catch(e => context.error(e));
      },
      setPosts(vuexContext, posts) {
        vuexContext.commit("setPosts", posts);
      }
    },

但是我收到一个错误提示 RangeError 超出最大调用堆栈大小

谁能帮我解决这个错误,我试图获取的数据就是这个对象:

{"-Lcq_6OaDEff_FPWoMux":{"author":"Ayman Tarig","content":"new nuxt blog","thumbnailLink":"http://smarterware.org/wp-content/uploads/2016/09/technology1.jpg","title":"new nuxt blog"}}

【问题讨论】:

  • 请在codesandbox上创建一个复制品

标签: firebase vue.js axios nuxt.js


【解决方案1】:

我尝试运行您的代码,它运行良好 - 我所做的更改是 -

使用单独的导出this.$axios

export const state = () => ({
  loadedPosts: []
})

export const mutations = {
  setPosts(state, posts) {
    state.loadedPosts = posts;
  }
}

export const actions = {
  nuxtServerInit(vuexContext, context) {      
    // return this.$axios <----CHANGE I MADE
    // code removed for brevity    
  },
  setPosts(vuexContext, posts) {
    vuexContext.commit("setPosts", posts);
  }
}

而且效果非常好 - 附上我的屏幕截图

【讨论】:

  • 可以给我store/index.js的完整代码吗?
  • 你在哪里导入 axios ?
  • @AymanTarig ,上面的代码是一样的!你用过-this.axios,我用过this.$axios。按照此处的说明设置 axios - axios.nuxtjs.org/setup
  • 我使用了与您提供的 url 中解释的相同设置,但我仍然遇到相同的错误。您可以在此链接github.com/Ayman-Tarig/nuxt-errors 中检查我的 store/index.js 和 nuxt.config.js 文件并告诉我它们有什么问题
  • 我使用的是 nuxtjs 版本 2.6.2
猜你喜欢
  • 1970-01-01
  • 2011-08-31
  • 1970-01-01
  • 2019-09-04
  • 2019-07-19
  • 1970-01-01
  • 1970-01-01
  • 2021-08-07
  • 2021-09-21
相关资源
最近更新 更多