2016-08-11 61 views
1

使用生成器時出現問題。我收到以下錯誤控制檯:控制檯中的JavaScript生成器語法錯誤

ERROR in ./app/redux/sagas/tracking.saga.js Module build failed: SyntaxError: C:/Workspace/teamable-frontend/app/redux/sagas/tracking.saga.js: Unexpected token (18:4)

這裏是package.json

{ 
"devDependencies": { 
    "autoprefixer-loader": "^3.2.0", 
    "babel-cli": "^6.4.5", 
    "babel-core": "^6.4.5", 
    "babel-loader": "^6.2.1", 
    "babel-plugin-transform-runtime": "^6.12.0", 
    "babel-polyfill": "^6.9.1", 
    "babel-preset-es2015": "^6.3.13", 
    "babel-preset-react": "^6.3.13", 
    "babel-preset-stage-0": "^6.5.0", 
    "babel-runtime": "^6.11.6", 
    ... 
    } 
... 
} 

和裝載在webpack.config

module: { 
    loaders: [{ 
     test: /.jsx?$/, 
     loader: 'babel-loader', 
     exclude: /node_modules/, 
      query: { 
      presets: ['es2015', 'react', 'stage-0'], 
      plugins: ["transform-runtime"] 
      } 
     }, 
    ... 
} 

而使用發電機功能:

import {put, call} from 'redux-saga/effects'; 
import {takeEvery} from 'redux-saga'; 

import {LOAD} from '../../constants/ActionTypes'; 
import {loadTrackingItemsSuccess, loadTrackingItemsFail} from '../actions/tracking.actions'; 
import {getTrackingItems} from '../../mocks/ListMock' 

function* loadTrackingItems() { 
    try { 
     const trackingItems = yield call(getTrackingItems); 
     yield put(loadTrackingItemsSuccess(trackingItems)); 
    } catch(ex) { 
     yield put(loadTrackingItemsFail(ex.toString())); 
    } 
} 

export function watchTrackingItemsLoad() { 
    yield* takeEvery(LOAD, loadTrackingItems); 
} 

我做錯了什麼?

回答

4

yieldyield*只能在發生器功能中使用。這裏:

export function watchTrackingItemsLoad() { 
    yield* takeEvery(LOAD, loadTrackingItems); 
} 

你正在使用yield*裏面的一個正常功能。這個函數也應該是一個生成器(function* watchTrackingItemsLoad),或者你應該返回生成器對象並讓調用者處理它(return takeEvery(LOAD, loadTrackingItems);)。

+0

Ooooh男人,多麼愚蠢的錯誤!非常感謝你! –