Maximum call stack size exceeded vue 2

huangapple go评论64阅读模式
英文:

Maximum call stack size exceeded vue 2

问题

我在尝试根据用户的roles_id值将用户重定向到其路径时遇到了这个问题

const getUserRole = (to, from, next) => {
  const currentUser = JSON.parse(localStorage.getItem("userData"));
  if (currentUser) {
    if (currentUser.roles_id === 1 && to.name === "user-dashboard") {
      next({
        name: "dashboard-new"
      });
    } else if (currentUser.roles_id === 2 && to.name === "dashboard-new") {
      next({
        name: "user-dashboard"
      });
    } else if (currentUser.roles_id === 3 && to.name === "mails-track") {
      next({
        name: "dispatch-dashboard"
      });
    } else if (to.name === "dashboard-new" && currentUser.roles_id === 1) {
      next();
    } else if (to.name === "user-dashboard" && currentUser.roles_id === 2) {
      next();
    } else if (to.name === "dispatch-dashboard" && currentUser.roles_id === 3) {
      next();
    } else {
      next("/");
    }
  } else {
    next("/");
  }
};

export default [
  {
    path: "/dashboard",
    name: "dashboard-new",
    component: () =>
      import("@/views/dashboard/new/DashboardNew.vue"),
    meta: {
      requiresAuth: true
    },
    beforeEnter: (to, from, next) => {
      const currentUser = JSON.parse(localStorage.getItem("userData"));
      if (currentUser && currentUser.roles_id === 1) {
        getUserRole(to, from, next);
      } else {
        next("/");
      }
    }
  },
  {
    path: "/user-dashboard",
    name: "user-dashboard",
    component: () =>
      import("@/views/dashboard/new/user/UserDashboard.vue"),
    meta: {
      requiresAuth: true
    },
    beforeEnter: (to, from, next) => {
      const currentUser = JSON.parse(localStorage.getItem("userData"));
      if (currentUser && currentUser.roles_id === 2) {
        getUserRole(to, from, next);
      } else {
        next("/");
      }
    }
  }
];

但是我得到了"Maximum call stack size exceeded"的错误,我尝试使用switch条件也不起作用。

在本地环境上运行正常,但一旦上传到主机上就出现了这个问题。

我的代码有什么问题吗?

英文:

I am facing this problem when i try to redirect the user to their path depends on their roles_id value

<!-- language: lang-js -->

const getUserRole = (to, from, next) =&gt; {
const currentUser = JSON.parse(localStorage.getItem(&quot;userData&quot;));
if (currentUser) {
if (currentUser.roles_id === 1 &amp;&amp; to.name === &quot;user-dashboard&quot;) {
next({
name: &quot;dashboard-new&quot;
});
} else if (currentUser.roles_id === 2 &amp;&amp; to.name === &quot;dashboard-new&quot;) {
next({
name: &quot;user-dashboard&quot;
});
} else if (currentUser.roles_id === 3 &amp;&amp; to.name === &quot;mails-track&quot;) {
next({
name: &quot;dispatch-dashboard&quot;
});
} else if (to.name === &quot;dashboard-new&quot; &amp;&amp; currentUser.roles_id === 1) {
next();
} else if (to.name === &quot;user-dashboard&quot; &amp;&amp; currentUser.roles_id === 2) {
next();
} else if (to.name === &quot;dispatch-dashboard&quot; &amp;&amp; currentUser.roles_id === 3) {
next();
} else {
next(&quot;/&quot;);
}
} else {
next(&quot;/&quot;);
}
};
export default [{
path: &quot;/dashboard&quot;,
name: &quot;dashboard-new&quot;,
component: () =&gt;
import (&quot;@/views/dashboard/new/DashboardNew.vue&quot;),
meta: {
requiresAuth: true
},
beforeEnter: (to, from, next) =&gt; {
const currentUser = JSON.parse(localStorage.getItem(&quot;userData&quot;));
if (currentUser &amp;&amp; currentUser.roles_id === 1) {
getUserRole(to, from, next);
} else {
next(&quot;/&quot;);
}
}
},
{
path: &quot;/user-dashboard&quot;,
name: &quot;user-dashboard&quot;,
component: () =&gt;
import (&quot;@/views/dashboard/new/user/UserDashboard.vue&quot;),
meta: {
requiresAuth: true
},
beforeEnter: (to, from, next) =&gt; {
const currentUser = JSON.parse(localStorage.getItem(&quot;userData&quot;));
if (currentUser &amp;&amp; currentUser.roles_id === 2) {
getUserRole(to, from, next);
} else {
next(&quot;/&quot;);
}
}
}
];

But I am getting "Maximum call stack size exceeded" i tried to use switch condition and also didn't work

It is working fine on local but once i upload it to my host it show this problem

Is there anything wrong with my code?

答案1

得分: 0

在调试之后,我发现在本地环境中,currentUser.roles_id === 1 会返回数字1,但在主机环境中会返回字符串。

所以在这种情况下,我不需要指定'==='中的类型,所以我将其更改为'==',只检查值。

英文:

After a debugging i found that in local currentUser.roles_id === 1 will return 1 as number but in host will return it as string

so in this case i don't need to specify the type in '===' so i changed it to '==' to check only the value

huangapple
  • 本文由 发表于 2023年2月8日 16:54:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定