Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.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 从Redux存储中获取未定义_Javascript_Reactjs_Redux - Fatal编程技术网

Javascript 从Redux存储中获取未定义

Javascript 从Redux存储中获取未定义,javascript,reactjs,redux,Javascript,Reactjs,Redux,当我加载页面时,我得到 “未捕获(承诺中)TypeError:无法从reducer.js中读取未定义的属性'data' 问题是Actions.getResults()返回未定义的。我深入到actions/api.js函数makeRequest(),它发出请求并获取数据 console.log(responseAction)之前的store.dispatch(responseAction)显示它拥有所有数据,但Actions.getResults()返回未定义的数据 我怀疑它可能与异步操作有关,但

当我加载页面时,我得到

“未捕获(承诺中)TypeError:无法从reducer.js中读取未定义的属性'data'

问题是
Actions.getResults()
返回未定义的。我深入到actions/api.js
函数makeRequest()
,它发出请求并获取数据

console.log(responseAction)
之前的
store.dispatch(responseAction)
显示它拥有所有数据,但
Actions.getResults()
返回未定义的数据

我怀疑它可能与异步操作有关,但我不知道到底是什么错了。任何帮助都将不胜感激

index.jsx

reducers/results.js

/actions/api.js

store.js

1)
getResults
方法不返回任何内容。它应该回报收到结果的承诺

2)
SearchPage
构造函数是同步的,所以您尝试同步获取异步函数的结果。在这里,您将创建请求-然后尝试从存储获取状态,之后所有请求都将完成并更新您的存储

const Actions = require('./actions');
const React = require('react');
const Results = require('./results.jsx');
const Store = require('./store');
const Qs = require('qs');

class SearchPage extends React.Component {
constructor(props) {
    super(props);
    const query = Qs.parse(this.props.location.search.substring(1));

    Actions.getResults(query);

    this.els = {};
    this.state = Store.getState();
}

componentWillReceiveProps(nextProps) {
    const query = Qs.parse(nextProps.location.search.substring(1));
    Actions.getResults(query);
}

componentDidMount() {

    this.unsubscribeStore = Store.subscribe(this.onStoreChange.bind(this));
}

onStoreChange() {
    this.setState(Store.getState());
}
render() {
    return (                  
            <Results data={this.state.results.data} />        
    );
}
}
module.exports = SearchPage;
const ApiActions = require('../../../../actions/api');
const Constants = require('./constants');
const Store = require('./store');

class Actions {
static getResults(data) {

    ApiActions.get(
        '/api/admins',
        data,
        Store,
        Constants.GET_RESULTS,
        Constants.GET_RESULTS_RESPONSE
    );
}
module.exports = Actions;
const Constants = require('../constants');

const initialState = {
hydrated: false,
loading: false,
error: undefined,
data: [],
pages: {},
items: {}
};
const reducer = (state = initialState, action) => {

switch (action.type) {

    case Constants.GET_RESULTS:
        return Object.assign({}, state, {
            hydrated: false,
            loading: true
        });
    case Constants.GET_RESULTS_RESPONSE:
        return Object.assign({}, state, {
            hydrated: true,
            loading: false,
     error>>data: action.response.data,
            pages: action.response.pages,
            items: action.response.items
        });

    default:
        return state;
}
};


module.exports = reducer;
const JsonFetch = require('../helpers/json-fetch');

class Actions {
static get(url, query, store, requestType, responseType) {

    const request = { method: 'GET', url, query };

    return this.makeRequest(request, store,requestType, responseType);
}



static async makeRequest(request, store, requestType, responseType) {

    store.dispatch({ type: requestType, request });

    const responseAction = { type: responseType };

    try {
        responseAction.data = await JsonFetch(request);
    }
    catch (error) {
        console.warn(`API: ${request.method} ${request.url}:`, error);

        responseAction.error = error;
    }

 >>>> console.log(responseAction) = Object
    store.dispatch(responseAction);
 >>>> console.log(responseAction) = nothing

    return responseAction;
}
}
module.exports = Actions;
const Redux = require('redux');
const Results = require('./reducers/results');


module.exports = Redux.createStore(
Redux.combineReducers({
    results: Results
})
);