2016-08-27 16 views
5

Tôi mới dùng phản ứng redux. Chỉ cần thử Redux Saga và gặp phải một số thông báo lỗi mà tôi không thể hiểu được. Đây là rootSaga tôi:Redux saga "rootSaga đã bị hủy"

import { takeEvery } from 'redux-saga'; 
import { AUTH_LOAD_SUCCESS,AUTH_SIGNUP,AUTH_SIGNIN,AUTH_SIGNOUT } from '../reducers/auth'; 
import { receiveAuth,signUp,signIn,onSignOut } from './auth'; 

export default function* rootSaga(){ 
    yield [ 
     takeEvery('AUTH_LOAD_SUCCESS', receiveAuth), 
     takeEvery('AUTH_SIGNUP', signUp), 
     takeEvery('AUTH_SIGNOUT',onSignOut), 
     takeEvery('AUTH_SIGNIN', signIn)   
] 

}

signin và đăng ký hoạt động hoàn hảo đối với tôi nhưng sau đó tôi nhận được thông báo lỗi sau đây trong giao diện điều khiển của tôi. Vì vậy, mặc dù tôi có thể đăng nhập bằng cách sử dụng redux-saga nhưng tôi không thể có được SignOut để làm việc có lẽ do saga bị "hủy bỏ" theo thông báo lỗi. Ai đó có thể giải thích cho tôi chuyện gì đang diễn ra không? Cảm ơn

bundle.js:79457 uncaught at check call: argument fn is undefinedlog @ bundle.js:79457 
bundle.js:79457 rootSaga has been cancelled 
bundle.js:79457 uncaught at rootSaga 
at rootSaga 
at signIn 
Error: call: argument fn is undefined 
    at check (http://127.0.0.1:3000/dist/bundle.js:79313:12) 
    at getFnCallDesc (http://127.0.0.1:3000/dist/bundle.js:80311:21) 
    at call (http://127.0.0.1:3000/dist/bundle.js:80336:24) 
    at signIn$ (http://127.0.0.1:3000/dist/bundle.js:81213:47) 
    at tryCatch (http://127.0.0.1:3000/dist/bundle.js:7893:41) 
    at GeneratorFunctionPrototype.invoke [as _invoke] (http://127.0.0.1:3000/dist/bundle.js:8167:23) 
    at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (http://127.0.0.1:3000/dist/bundle.js:7926:22) 
    at next (http://127.0.0.1:3000/dist/bundle.js:79727:28) 
    at currCb (http://127.0.0.1:3000/dist/bundle.js:79801:8) 
    at http://127.0.0.1:3000/dist/bundle.js:79904:17 

thêm thông tin:

sagas/auth.js 
import 'babel-polyfill'; 
import fetch from 'isomorphic-fetch' 
import { browserHistory } from 'react-router'; 
import cookie from 'react-cookie'; 
import { put,call,select } from 'redux-saga/effects'; 
import { requestSignUp,requestSignIn,requestSignOut,receiveUser,receiveSignIn,receiveSignOut } from '../reducers/auth' 



export function* onSignOut(){ 

/////////////////ignore this for the moment/////// 
console.log("ffss") 

} 

export function* receiveAuth() { 
    const user = cookie.load('username'); 

    yield put(receiveAuth(user)); 
    } 

export function* signUp(action) { 
    yield put(requestSignUp()) 
    try { 
     const response = yield call(fetch,'/api/sign_up', { 
        method:'POST', 
        headers:{'Accept': 'application/json', 'Content-Type': 'application/json'}, 
        body: JSON.stringify(action.payload) /////action.payload contains {username:'aa',password:'aa'} 
        }) 
     if(response.ok){ 
      yield call(cookie.save,'username', action.payload.username) 
      yield put(receiveUser(action.payload.username)) 
      yield call(browserHistory.push('/chat')) 
     } 
    } catch (err){ 
     throw err 
     }  
} 

export function* signIn(action) { console.log("hi") 
    yield put(requestSignIn()) 
    try { 

     const response = yield call(fetch,'/api/sign_in', { 
       method:'POST', 
       headers:{'Accept': 'application/json', 'Content-Type': 'application/json'}, 
       body: JSON.stringify(action.payload) /////action.payload contains {username:'aa',password:'aa'} 
       }) 


     if(response.ok){ 
      yield call(cookie.save,'username',action.payload.username) 
      yield put(receiveSignIn(action.payload)) 
      yield call(browserHistory.push('/chat')) 
     } 
    } catch (err){ 
     throw err 
     } 
} 

Tôi nghĩ rằng tôi đang dự kiến ​​sẽ đưa vào một cuộc tranh luận thứ hai để gọi nhưng khi tôi mã vô nghĩa yield call(browserHistory.push('/chat'),null) tôi vẫn nhận được lỗi

uncaught at signIn call: argument fn is undefined 
uncaught at check call: argument fn is undefined 

Ngoài ra, receiveAuth của tôi đang trả về thông báo này:

uncaught at rootSaga 
at rootSaga 
at receiveAuth 
Error: Actions must be plain objects. Use custom middleware for async actions. 
+0

Bạn có thể gửi mã cho saga signin? có vẻ như đó là nơi ngoại lệ bị ném –

Trả lời

11

Lỗi xuất phát từ một số yield call(somefunc, ...) bạn có trong câu chuyện signIn. Nó nói rằng someFunc (bất kể tên nào có trong mã của bạn) là không xác định. Vì vậy, bạn nên kiểm tra xem bạn havent gõ sai tên hàm hoặc có thể bỏ qua để xuất khẩu nó từ mô-đun của nó xác định

EDIT

tôi nhận thấy rằng bạn gọi hàm browserHistory.push như thế này

yield call(browserHistory.push('/chat'),null) 

Bạn không nên tự gọi hàm đó nhưng cung cấp chức năng chính nó cùng với các đối số (và cuối cùng là bối cảnh này) đến saga

yield apply(browserHistory, browserHistory.push, ['/chat']) 

Tôi đang sử dụng apply Effect cho phép gọi phương pháp với một bối cảnh this (bối cảnh được cung cấp như một đối số 1)

+0

Có hai cuộc gọi có thể gây ra sự cố. Vui lòng xem thêm thông tin tôi đã thêm. Bạn có thể cho tôi biết làm thế nào họ cần được sửa chữa? – mtangula

+0

Hoạt động hoàn hảo, cảm ơn! BTW Tôi thấy bạn là một fan hâm mộ của Naruto – mtangula

+0

Oh Yeeeeeeeees! –

Các vấn đề liên quan