Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ReactJS+;NodeJS:“;TypeError:无法读取属性';推动';“未定义”的定义;当用户会话到期时_Javascript_Node.js_Reactjs_Redux - Fatal编程技术网

Javascript ReactJS+;NodeJS:“;TypeError:无法读取属性';推动';“未定义”的定义;当用户会话到期时

Javascript ReactJS+;NodeJS:“;TypeError:无法读取属性';推动';“未定义”的定义;当用户会话到期时,javascript,node.js,reactjs,redux,Javascript,Node.js,Reactjs,Redux,我为我的应用程序实现了一个简单的身份验证层,每次用户会话到期时,我都会收到此错误消息,而不是在主页上重定向。 redux/actions/authentication.js: import axios from 'axios'; import { GET_ERRORS, SET_CURRENT_USER } from './types'; // we list here the actions we'll use import setAuthToken from '../../setAuthT

我为我的应用程序实现了一个简单的身份验证层,每次用户会话到期时,我都会收到此错误消息,而不是在主页上重定向。

redux/actions/authentication.js

import axios from 'axios';
import { GET_ERRORS, SET_CURRENT_USER } from './types'; // we list here the actions we'll use
import setAuthToken from '../../setAuthToken';
import jwt_decode from 'jwt-decode';

export const registerUser = (user, history) => dispatch => {
    axios.post('/api/users/register', user)
            .then(res => history.push('/login'))
            .catch(err => {
                dispatch({
                    type: GET_ERRORS,
                    payload: err.response.data
                });
            });
}

export const loginUser = (user) => dispatch => {
    axios.post('/api/users/login', user)
        .then(res => {
            //console.log(res.data);
            const { token } = res.data;
            localStorage.setItem('jwtToken', token);
            setAuthToken(token);
            const decoded = jwt_decode(token);
            dispatch(setCurrentUser(decoded));
        })
        .catch(err => {
            dispatch({
                type: GET_ERRORS,
                payload: err.response.data
            });
        });
}

export const setCurrentUser = decoded => {
    return {
        type: SET_CURRENT_USER,
        payload: decoded
    }
}

export const logoutUser = (history) => dispatch => {
    localStorage.removeItem('jwtToken');
    setAuthToken(false);
    dispatch(setCurrentUser({}));
    history.push('/login');
}
src/App.js(相关部分):

if(localStorage.jwtToken){
setAuthToken(localStorage.jwtToken);
const decoded=jwt_decode(localStorage.jwtToken);
存储调度(setCurrentUser(已解码));
const currentTime=Date.now()/1000;
if(decoded.exp

/login
页面已存在。出现此错误时-我可以刷新页面,应用程序将在
/
上重定向我。为什么会发生这种情况?

在商店中注册logoutUser时,必须传递历史记录对象

if(decoded.exp < currentTime) {
    store.dispatch(logoutUser(history));
    window.location.href = '/login'
}
if(decoded.exp

否则它将是未定义的

如果历史记录处于您的状态,那么您可以使用getState()将其拉回。如果它不在getState中,那么如上所述,您必须将该值传递到您的logoutUser调用中

if(localStorage.jwtToken){
setAuthToken(localStorage.jwtToken);
const decoded=jwt_decode(localStorage.jwtToken);
存储调度(setCurrentUser(已解码));
const currentTime=Date.now()/1000;
if(decoded.exp(dispatch,getState)=>{
localStorage.removietem('jwtToken');
setAuthToken(false);
分派(setCurrentUser({}));
getState().user.history.push('/login');//将用户替换为状态中的历史记录

}
您可以添加完整的App.js complete吗?当您调用logoutUser()时,您没有传入任何内容。因此,在函数内部,历史记录将是未定义的。更像是
logoutUser(history)(dispatch)
我尝试了这里概述的两种方法,但由于
意外使用了“history”而失败了,没有限制的全局变量。我是否需要以某种方式将
history
加载到这里?使用
store.dispatch(this.props.history)测试它。历史记录是否在状态对象上?如果是这样,那么您不需要将其传递给操作,您只需使用getState()函数,使用
store.dispatch(logoutUser(this.props.history))
返回此错误:
TypeError:无法读取未定义的属性“history”
@你能给我举个例子吗?谢谢你好@Ageoff,谢谢你的回答。我对它进行了测试,当我尝试注销时,我收到了以下错误消息:
uncaughttypeerror:无法读取未定义的属性“history”
。我还尝试将
历史记录
作为参数添加到
注销用户
,但也没有帮助。
if(decoded.exp < currentTime) {
    store.dispatch(logoutUser(history));
    window.location.href = '/login'
}