Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.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_Redux Thunk - Fatal编程技术网

Javascript 反应+;Redux:未捕获错误:预期减速器为函数

Javascript 反应+;Redux:未捕获错误:预期减速器为函数,javascript,reactjs,redux,redux-thunk,Javascript,Reactjs,Redux,Redux Thunk,我收到一条错误消息,但我不确定如何解决它: Uncaught Error: Expected the reducer to be a function 以下是我的react/redux工作流的各个部分: 根部还原剂 import { combineReducers } from 'redux'; import PostsReducer from './reducer_posts'; import StreakReducer from './reducer_streak'; import { r

我收到一条错误消息,但我不确定如何解决它:

Uncaught Error: Expected the reducer to be a function
以下是我的react/redux工作流的各个部分:

根部还原剂

import { combineReducers } from 'redux';
import PostsReducer from './reducer_posts';
import StreakReducer from './reducer_streak';
import { reducer as formReducer } from 'redux-form';

const rootReducer = combineReducers({
  entries: PostsReducer,
  form: formReducer,
  streak: StreakReducer
});

export default rootReducer;
import axios from 'axios';
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from '../reducers/index';

const store = createStore(
  rootReducer,
  applyMiddleware(thunk)
);

export const FETCH_ENTRIES = 'FETCH_ENTRIES';
export const FETCH_LONGEST = 'FETCH_LONGEST';

const ROOT_URL = 'http://localhost:3000/entries';

export function fetchEntries() {
  const request = axios.get(`${ROOT_URL}`);
  return {
    type: FETCH_ENTRIES,
    payload: request
  };
}


// action creator for returning the longest streak of rejections
export function longestStreak() {
  return function(dispatch) {
    return fetchEntries().then(
      entries => dispatch(findLongestStreak(entries))
    );
  };
}


function findLongestStreak(entries) {
  var longestStreakLength = 10;
  return {
    type: FETCH_LONGEST,
    payload: longestStreakLength
  };
}
import { FETCH_LONGEST } from '../actions/index';

const INITIAL_STATE = { streak: 0 };

export default function(state = INITIAL_STATE, action) {
  switch(action.type) {
    case FETCH_LONGEST:
      return { ...state, streakInfo: action.payload.data};
    default:
      return state;
  }
}
import { FETCH_ENTRIES, CREATE_ENTRY } from '../actions/index';

const INITIAL_STATE = { all: [], entry: null };

export default function(state = INITIAL_STATE, action) {
  switch(action.type) {
    case CREATE_ENTRY:
      return {...state, entry: action.payload.data };
    case FETCH_ENTRIES:
      return { ...state, all: action.payload.data };
    default:
      return state;
  }
}
动作创建者

import { combineReducers } from 'redux';
import PostsReducer from './reducer_posts';
import StreakReducer from './reducer_streak';
import { reducer as formReducer } from 'redux-form';

const rootReducer = combineReducers({
  entries: PostsReducer,
  form: formReducer,
  streak: StreakReducer
});

export default rootReducer;
import axios from 'axios';
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from '../reducers/index';

const store = createStore(
  rootReducer,
  applyMiddleware(thunk)
);

export const FETCH_ENTRIES = 'FETCH_ENTRIES';
export const FETCH_LONGEST = 'FETCH_LONGEST';

const ROOT_URL = 'http://localhost:3000/entries';

export function fetchEntries() {
  const request = axios.get(`${ROOT_URL}`);
  return {
    type: FETCH_ENTRIES,
    payload: request
  };
}


// action creator for returning the longest streak of rejections
export function longestStreak() {
  return function(dispatch) {
    return fetchEntries().then(
      entries => dispatch(findLongestStreak(entries))
    );
  };
}


function findLongestStreak(entries) {
  var longestStreakLength = 10;
  return {
    type: FETCH_LONGEST,
    payload: longestStreakLength
  };
}
import { FETCH_LONGEST } from '../actions/index';

const INITIAL_STATE = { streak: 0 };

export default function(state = INITIAL_STATE, action) {
  switch(action.type) {
    case FETCH_LONGEST:
      return { ...state, streakInfo: action.payload.data};
    default:
      return state;
  }
}
import { FETCH_ENTRIES, CREATE_ENTRY } from '../actions/index';

const INITIAL_STATE = { all: [], entry: null };

export default function(state = INITIAL_STATE, action) {
  switch(action.type) {
    case CREATE_ENTRY:
      return {...state, entry: action.payload.data };
    case FETCH_ENTRIES:
      return { ...state, all: action.payload.data };
    default:
      return state;
  }
}
条纹减少器

import { combineReducers } from 'redux';
import PostsReducer from './reducer_posts';
import StreakReducer from './reducer_streak';
import { reducer as formReducer } from 'redux-form';

const rootReducer = combineReducers({
  entries: PostsReducer,
  form: formReducer,
  streak: StreakReducer
});

export default rootReducer;
import axios from 'axios';
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from '../reducers/index';

const store = createStore(
  rootReducer,
  applyMiddleware(thunk)
);

export const FETCH_ENTRIES = 'FETCH_ENTRIES';
export const FETCH_LONGEST = 'FETCH_LONGEST';

const ROOT_URL = 'http://localhost:3000/entries';

export function fetchEntries() {
  const request = axios.get(`${ROOT_URL}`);
  return {
    type: FETCH_ENTRIES,
    payload: request
  };
}


// action creator for returning the longest streak of rejections
export function longestStreak() {
  return function(dispatch) {
    return fetchEntries().then(
      entries => dispatch(findLongestStreak(entries))
    );
  };
}


function findLongestStreak(entries) {
  var longestStreakLength = 10;
  return {
    type: FETCH_LONGEST,
    payload: longestStreakLength
  };
}
import { FETCH_LONGEST } from '../actions/index';

const INITIAL_STATE = { streak: 0 };

export default function(state = INITIAL_STATE, action) {
  switch(action.type) {
    case FETCH_LONGEST:
      return { ...state, streakInfo: action.payload.data};
    default:
      return state;
  }
}
import { FETCH_ENTRIES, CREATE_ENTRY } from '../actions/index';

const INITIAL_STATE = { all: [], entry: null };

export default function(state = INITIAL_STATE, action) {
  switch(action.type) {
    case CREATE_ENTRY:
      return {...state, entry: action.payload.data };
    case FETCH_ENTRIES:
      return { ...state, all: action.payload.data };
    default:
      return state;
  }
}
Posts Reducer

import { combineReducers } from 'redux';
import PostsReducer from './reducer_posts';
import StreakReducer from './reducer_streak';
import { reducer as formReducer } from 'redux-form';

const rootReducer = combineReducers({
  entries: PostsReducer,
  form: formReducer,
  streak: StreakReducer
});

export default rootReducer;
import axios from 'axios';
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from '../reducers/index';

const store = createStore(
  rootReducer,
  applyMiddleware(thunk)
);

export const FETCH_ENTRIES = 'FETCH_ENTRIES';
export const FETCH_LONGEST = 'FETCH_LONGEST';

const ROOT_URL = 'http://localhost:3000/entries';

export function fetchEntries() {
  const request = axios.get(`${ROOT_URL}`);
  return {
    type: FETCH_ENTRIES,
    payload: request
  };
}


// action creator for returning the longest streak of rejections
export function longestStreak() {
  return function(dispatch) {
    return fetchEntries().then(
      entries => dispatch(findLongestStreak(entries))
    );
  };
}


function findLongestStreak(entries) {
  var longestStreakLength = 10;
  return {
    type: FETCH_LONGEST,
    payload: longestStreakLength
  };
}
import { FETCH_LONGEST } from '../actions/index';

const INITIAL_STATE = { streak: 0 };

export default function(state = INITIAL_STATE, action) {
  switch(action.type) {
    case FETCH_LONGEST:
      return { ...state, streakInfo: action.payload.data};
    default:
      return state;
  }
}
import { FETCH_ENTRIES, CREATE_ENTRY } from '../actions/index';

const INITIAL_STATE = { all: [], entry: null };

export default function(state = INITIAL_STATE, action) {
  switch(action.type) {
    case CREATE_ENTRY:
      return {...state, entry: action.payload.data };
    case FETCH_ENTRIES:
      return { ...state, all: action.payload.data };
    default:
      return state;
  }
}
组件

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { longestStreak } from '../actions/index';


class EntriesMetadata extends Component {
  componentWillMount() {
    this.props.longestStreak();
  }

  render() {
    return (
        <div>
      </div>
    );
    }
  }

function mapStateToProps(state) {
  return { streak: state.streak.streakInfo };
}

export default connect(mapStateToProps, { longestStreak })(EntriesMetadata);
import React,{Component}来自'React';
从'react redux'导入{connect};
从“../actions/index”导入{longeststrick};
类EntriesMetadata扩展组件{
组件willmount(){
this.props.longestreak();
}
render(){
返回(
);
}
}
函数MapStateTops(状态){
返回{streak:state.streak.streakInfo};
}
导出默认连接(MapStateTops,{longestStreak})(EntriesMetadata);
我不知道如何解决这个问题。谁能给我指出正确的方向吗?这是我的第一个redux项目!如果需要其他代码片段,请告诉我

编辑

这是我的github回购:

请尝试以下操作: 将名称添加到减速器函数:

export default function PostsReducer(state = INITIAL_STATE, action) {
  switch(action.type) {
    case CREATE_ENTRY
: 对StreakReducer执行相同的操作。 我检查了自己的项目,这是我的解决方案和您的解决方案之间的唯一区别。

请尝试以下操作: 将名称添加到减速器函数:

export default function PostsReducer(state = INITIAL_STATE, action) {
  switch(action.type) {
    case CREATE_ENTRY
: 对StreakReducer执行相同的操作。
我检查了自己的项目,这是我的解决方案和您的解决方案之间的唯一区别。

您需要修复
actions/index.js
中的代码。它当前重新导入减缩器,然后尝试创建存储,而存储不应该存在。当两个还原程序都导入此文件时,它们最终会触发循环依赖关系


清理操作代码后,您需要修复其他代码错误。

您需要修复
actions/index.js中的代码。它当前重新导入减缩器,然后尝试创建存储,而存储不应该存在。当两个还原程序都导入此文件时,它们最终会触发循环依赖关系


清理操作代码后,您需要修复其他代码错误。

您忘记了mapDispatchToProps。您应该使用mapDispatchToProps传递您的操作,然后从props(this.props.actions.longestStreak())访问它


你忘了mapDispatchToProps。您应该使用mapDispatchToProps传递您的操作,然后从props(this.props.actions.longestStreak())访问它


我不明白你为什么要在你的操作中创建存储,你已经在index.js中这样做了,对吗

如果您从操作文件中注释掉存储代码,它应该可以正常工作。看起来像是循环依赖

在您的存储库上创建了带有更改的PR。检查一下


如果您遇到任何问题,请务必告诉我。这是PR,我无法理解您为什么要在您的操作中创建存储,您已经在index.js中这样做了,对吗

如果您从操作文件中注释掉存储代码,它应该可以正常工作。看起来像是循环依赖

在您的存储库上创建了带有更改的PR。检查一下


如果您遇到任何问题,请告诉我。这是PR

您能显示两个减速机的代码吗?请同时发布其他减速机的代码。如果您可以共享github链接,您可能会得到更快的答案。@我在原始帖子中发布了此项目的github链接。您可以显示两个还原器的代码吗?请同时发布其他还原器的代码。如果您可以共享github链接,您可能会得到更快的答案。@我在原始帖子中发布了此项目的github链接。javascript控制台仍然显示“未捕获错误:预期减速机是一个函数”。我已经发布了这个项目的github repo,作为对我文章的编辑,以防您想看到整个源代码。javascript控制台仍然显示“未捕获错误:预期减速机是一个函数”。我已经发布了这个项目的github repo,作为对我文章的编辑,以防您想查看整个源代码。我尝试了您建议的更改,控制台声明
return fetchEntries()。然后
src\actions\index.js中的
没有被识别为有效函数?我是否也需要手动将redux promise导入我的应用程序?您的服务器是否在端口3000上运行?我尝试了您建议的更改,控制台声明
return fetchEntries()。然后
src\actions\index.js中的
未被识别为有效函数?我是否也需要手动将redux promise导入我的应用程序?您的服务器是否在端口3000上运行?谢谢@Ruslan,我会尝试一下并让您知道!谢谢@Ruslan,我会试试看,让你知道!