简体   繁体   English

使用 Next js 时出现以下错误。 我正在使用 axios 作为 http 客户端构建身份验证管道

[英]I am getting the following error using Next js. I am building an authentication pipeline using axios as the http client

The error: Server Error ReferenceError: localStorage is not defined This error happened while generating the page.错误:Server Error ReferenceError: localStorage is not defined 生成页面时发生此错误。 Any console logs will be displayed in the terminal window.任何控制台日志都将显示在终端窗口中。

import { LOGIN_SUCCESS, LOGIN_FAIL, LOGOUT } from "../actions/types";

const user = JSON.parse(localStorage.getItem("user"));

const initialState = user
  ? { isLoggedIn: true, user }
  : { isLoggedIn: false, user: null };

export default function (state = initialState, action) {
  const { type, payload } = action;

  switch (type) {
    case LOGIN_SUCCESS:
      return {
        ...state,
        isLoggedIn: true,
        user: payload.user,
      };
    case LOGIN_FAIL:
      return {
        ...state,
        isLoggedIn: false,
        user: null,
      };
    case LOGOUT:
      return {
        ...state,
        isLoggedIn: false,
        user: null,
      };
    default:
      return state;
  }
}

It's related to server side rendering and client side rendering.它与服务器端渲染和客户端渲染有关。 As Next.js provides SSR, you need to consider using objects like window, localStorage and so on.由于 Next.js 提供了 SSR,所以需要考虑使用 window、localStorage 等对象。 While compiling client side, those objects are fine but when Nextjs compiles server side, it shows error like you shared.在编译客户端时,这些对象很好,但是当 Nextjs 编译服务器端时,它会显示您共享的错误。

You can use something like this:你可以使用这样的东西:

let user = null;
if (typeof window !== "undefined") {
  user = JSON.parse(localStorage.getItem("user"));
} else {
  // ... server-side logic
}

For other solutions see here: https://dev.to/vvo/how-to-solve-window-is-not-defined-errors-in-react-and-next-js-5f97对于其他解决方案,请参见此处: https : //dev.to/vvo/how-to-solve-window-is-not-defined-errors-in-react-and-next-js-5f97

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试将值从数据库传递给 js。 但是我收到了一个步进错误? - I am trying to pass values from Database to js. But i am getting an error of stepup? 为什么使用 axios 调用 api 时出现错误? - Why I am getting error when calling the api using axios? 我正在使用 axios 在 React 中制作 CRUD web 应用程序。 我收到以下错误:“TypeError: Object(...) is not a function” - I am making a CRUD web application in React using axios. I'm getting the following error: “TypeError: Object(…) is not a function” 我正在使用 NodeJs 构建电子商务应用程序。 但是,出现 axios 未定义错误 - I am using NodeJs for building e-commerce app. However, axios is not defined error occurs Angular JS。 我正在尝试使用复选框过滤结果的简单列表 - Angular JS. I am attempting to filter a simple list of results using check boxes 当我使用 createAsyncThunk 和 axios 时,我不断收到 400 错误 - I keep getting a 400 error when i am using createAsyncThunk and axios 为什么在angular js中使用resolve时出现错误? - why i am getting error while using resolve in angular js? 我正在为后端使用 Sanity CMS 构建 React 应用程序,但在向后端发送数据时遇到客户端错误 - I am building a React Application using the Sanity CMS for the backend but I am running into a client error when sending data to the backend UnhandledPromiseRejectionWarning:未处理的承诺拒绝错误,我正在使用 Node JS 和 mongo Db - UnhandledPromiseRejectionWarning: Unhandled promise rejection error I am getting I am using Node JS and mongo Db 我正在使用 Expo 构建一个我在其中进行 OTP 身份验证的应用程序 - I am using Expo for building an app in which i am working in OTP authentication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM