代码之家  ›  专栏  ›  技术社区  ›  ocuenca

使用redux saga,take(patternOrChannel)时出错:参数8不是有效通道或有效模式

  •  0
  • ocuenca  · 技术社区  · 7 年前

    我正在尝试使用redux saga库捕获一些操作,但在运行应用程序时出现以下错误:

    js:2178在rootSaga在rootSaga在projectSaga在 take上的watchFetchRequest每个错误:take(patternOrChannel): 在采取( http://localhost:3000/static/js/bundle.js:84689:9 ) http://localhost:3000/static/js/bundle.js:85993:94 ) 在createTaskIterator( http://localhost:3000/static/js/bundle.js:85179:17 ) 在Runfork效应下( http://localhost:3000/static/js/bundle.js:85583:24 ) 运行效应( http://localhost:3000/static/js/bundle.js:85468:872 ) http://localhost:3000/static/js/bundle.js:85348:9 在程序( http://localhost:3000/static/js/bundle.js:85303:3 ) 在Runfork效应下( http://localhost:3000/static/js/bundle.js:85587:19 ) 运行效应( http://localhost:3000/static/js/bundle.js:85468:872 ) 在 http://localhost:3000/static/js/bundle.js:85677:14 在runAllEffect( http://localhost:3000/static/js/bundle.js:85676:10 ) 运行效应( http://localhost:3000/static/js/bundle.js:85468:413 ) 在下一个( http://localhost:3000/static/js/bundle.js:85348:9 在程序( http://localhost:3000/static/js/bundle.js:85303:3 在Runfork效应下( http://localhost:3000/static/js/bundle.js:85587:19 ) 运行效应( http://localhost:3000/static/js/bundle.js:85468:872 ) 在 位于Array.forEach() http://localhost:3000/static/js/bundle.js:85676:10 ) 运行效应( ) 在下一个( http://localhost:3000/static/js/bundle.js:85348:9 在程序( http://localhost:3000/static/js/bundle.js:85303:3 ) 在runSaga( http://localhost:3000/static/js/bundle.js:85858:76 在对象处../src/store/ReduxRoot.tsx( http://localhost:3000/static/js/bundle.js:95823:16 ) 需要的网页包 http://localhost:3000/static/js/bundle.js:679:30 ) 在fn( http://localhost:3000/static/js/bundle.js:89:20 ) 位于对象../src/index.tsx( http://localhost:3000/static/js/bundle.js:95325:75 需要的网页包 ( http://localhost:3000/static/js/bundle.js:679:30 在fn( http://localhost:3000/static/js/bundle.js:89:20 ) 在对象0处( http://localhost:3000/static/js/bundle.js:96424:18 ) ( http://localhost:3000/static/js/bundle.js:725:37 http://localhost:3000/static/js/bundle.js:728:10

    这是传奇的密码:

    import { all, call, fork, put, takeEvery } from 'redux-saga/effects';
    import {  ActionType, Action, SearchCriteria } from '../model/types';
    import { searchProjectsError, searchProjectsCompleted } from '../actions/projects';
    import { API_URL } from '../../config';
    // import axios from 'axios';
    
    function callApi(method: string, url: string, path: string, data?: any) {
        return fetch(url  + path, {
          method,
          headers: {'Content-Type': 'application/json; charset=utf-8', 'Access-Control-Allow-Origin': '*'},
          body: JSON.stringify(data)
        }).then(res => res.json());
      }
    
    // Here we use `redux-saga` to trigger actions asynchronously. `redux-saga` uses something called a
    // "generator function", which you can read about here:
    // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*
    
    function* handleFetch(action: Action<SearchCriteria>) {
      try {
        // To call async functions, use redux-saga's `call()`.
        const res = yield call(callApi, 'get', API_URL , '/api/DocumentLoader/GetProjects/', action.payload);
    
        if (res.error) {
          yield put(searchProjectsError(res.error));
        } else {
          yield put(searchProjectsCompleted(res));
        }
      } catch (err) {
        if (err instanceof Error) {
          yield put(searchProjectsError(err.stack!));
        } else {
          yield put(searchProjectsError('An unknown error occured.'));
        }
      }
    }
    
    // This is our watcher function. We use `take*()` functions to watch Redux for a specific action
    // type, and run our saga, for example the `handleFetch()` saga above.
    function* watchFetchRequest() {
      yield takeEvery(ActionType.SEARCH_PROJECTS, handleFetch); // I think the error is here
    }
    
    // We can also use `fork()` here to split our saga into multiple watchers.
    function* projectSaga() {
      yield all([fork(watchFetchRequest)]);
    }
    
    export default projectSaga;
    

    我已经试着在这里找到答案,但我能找到的只有 this post ,但是 ActionType handleFetch 功能

    export function searchProjects(criterias: SearchCriteria): Action<SearchCriteria> {
        return {
            type: ActionType.SEARCH_PROJECTS,
            payload: criterias
        };
    }
    

    另一件事可能是我当时在编写传奇中间件时犯了错误:

    const sagaMiddleware = createSagaMiddleware();
    
    var middleware = applyMiddleware(logger, thunk, sagaMiddleware);
    
    if (process.env.NODE_ENV === 'development') {
        middleware = composeWithDevTools(middleware);
    }
    
    // Here we use `redux-saga` to trigger actions asynchronously. `redux-saga` uses something called a
    // "generator function", which you can read about here:
    // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*
    export function* rootSaga() {
        yield all([fork(projectSaga)]);
      }
    // running the root saga, and return the store object.
     sagaMiddleware.run(rootSaga);
    

    我还尝试从中删除action参数 手印 错误还在发生

    2 回复  |  直到 7 年前
        1
  •  13
  •   ocuenca    7 年前

    我发现什么是错误,问题是定义 ActionType 枚举。它没有为每个操作分配字符串值。

    export const enum ActionType {
    
    // Projects
    SEARCH_PROJECT,
    SEARCH_PROJECTS_COMPLETED,
    SEARCH_PROJECTS_ERROR,
    
    }
    

    这解决了问题:

    export const enum ActionType {
    
    // Projects
    SEARCH_PROJECTS= '@@projects/SEARCH_PROJECTS',
    SEARCH_PROJECTS_COMPLETED= '@@projects/SEARCH_PROJECTS_COMPLETED',
    SEARCH_PROJECTS_ERROR= '@@projects/SEARCH_PROJECTS_ERROR',
    
    }
    
        2
  •  0
  •   Manoj Kumar    7 年前

    检查是否从定义常量的models文件中导入了{ActionType}对象。

    export function searchProjects(criterias: SearchCriteria): 
        Action<SearchCriteria> {
        return {
            type: ActionType.SEARCH_PROJECTS,
            payload: criterias
        };
    }