在ReactJS代码中未定义的属性isAuthenticated处获取错误

时间:2019-07-10 00:10:31

标签: reactjs

我正在尝试使用具有注册和登录功能的ReactJS技术构建一个应用程序。寄存器功能运行正常。  但是,每当我单击“登录”选项时,它都会失败,并显示以下错误消息。

获取TypeError:在componentDidMount()函数中运行ReactJS代码时,无法读取未定义的属性'isAuthenticated'。下面是Login.js中的代码,单击“登录”选项即可调用该代码。

 class Login extends Component {
      constructor() {
        super();
     //   this.props = props;
        this.state = {
          email: "",
          password: "",
          errors: {}
        };
      }



      componentDidMount() {
        // If logged in and user navigates to Register page, should redirect them to dashboard
        if (this.props.auth.isAuthenticated) {
          this.props.history.push("/dashboard");
        }
      }
}

下面是我在控制台窗口中看到的错误消息。

Failed prop type: The prop `auth` is marked as required in `Login`, but its value is `undefined`.
    in Login (created by ConnectFunction)
    in ConnectFunction (created by Context.Consumer)
    in Route (at App.js:49)
    in div (at App.js:45)
    in Router (created by BrowserRouter)
    in BrowserRouter (at App.js:44)
    in Provider (at App.js:43)
    in App (at src/index.js:7)
console.<computed> @ index.js:1375
index.js:1375 Warning: Failed prop type: The prop `errors` is marked as required in `Login`, but its value is `undefined`.
    in Login (created by ConnectFunction)
    in ConnectFunction (created by Context.Consumer)
    in Route (at App.js:49)
    in div (at App.js:45)
    in Router (created by BrowserRouter)
    in BrowserRouter (at App.js:44)
    in Provider (at App.js:43)
    in App (at src/index.js:7)
console.<computed> @ index.js:1375
Login.js:29 Uncaught TypeError: Cannot read property 'isAuthenticated' of undefined
    at Login.componentDidMount (Login.js:29)
    at commitLifeCycles (react-dom.development.js:18109)
    at commitAllLifeCycles (react-dom.development.js:19668)
    at HTMLUnknownElement.callCallback (react-dom.development.js:147)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:196)
    at invokeGuardedCallback (react-dom.development.js:250)
    at commitRoot (react-dom.development.js:19892)
    at react-dom.development.js:21440
    at Object.unstable_runWithPriority (scheduler.development.js:255)
    at completeRoot (react-dom.development.js:21439)
    at performWorkOnRoot (react-dom.development.js:21362)
    at performWork (react-dom.development.js:21267)
    at performSyncWork (react-dom.development.js:21241)
    at interactiveUpdates$1 (react-dom.development.js:21526)
    at interactiveUpdates (react-dom.development.js:2268)
    at dispatchInteractiveEvent (react-dom.development.js:5085)
index.js:1375 The above error occurred in the <Login> component:
    in Login (created by ConnectFunction)
    in ConnectFunction (created by Context.Consumer)
    in Route (at App.js:49)
    in div (at App.js:45)
    in Router (created by BrowserRouter)
    in BrowserRouter (at App.js:44)
    in Provider (at App.js:43)
    in App (at src/index.js:7)

Consider adding an error boundary to your tree to customize error handling behavior.

console.<computed> @ index.js:1375
react-dom.development.js:21327 Uncaught TypeError: Cannot read property 'isAuthenticated' of undefined
    at Login.componentDidMount (Login.js:29)
    at commitLifeCycles (react-dom.development.js:18109)
    at commitAllLifeCycles (react-dom.development.js:19668)
    at HTMLUnknownElement.callCallback (react-dom.development.js:147)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:196)
    at invokeGuardedCallback (react-dom.development.js:250)
    at commitRoot (react-dom.development.js:19892)
    at react-dom.development.js:21440
    at Object.unstable_runWithPriority (scheduler.development.js:255)
    at completeRoot (react-dom.development.js:21439)
    at performWorkOnRoot (react-dom.development.js:21362)
    at performWork (react-dom.development.js:21267)
    at performSyncWork (react-dom.development.js:21241)
    at interactiveUpdates$1 (react-dom.development.js:21526)
    at interactiveUpdates (react-dom.development.js:2268)
    at dispatchInteractiveEvent (react-dom.development.js:5085)

0 个答案:

没有答案