2017-05-03 57 views
0

我有一個工作操作,通過簡單調用「saveJwt(data)」保存JWT令牌。反應減少 - 在調度中應用「提取」和調度失敗

這裏是動作:

export const requestLoginToken = (username, password) => 
    (dispatch, getState) => { 
    dispatch({type: REQUEST_LOGIN_TOKEN, payload: username}) 

    const payload = { 
     userName: username, 
     password: password, 
    } 

    const task = fetch('/api/jwt', { 
     method: 'POST', 
     body: JSON.stringify(payload), 
     headers: { 
     'Content-Type': 'application/json;charset=UTF-8' 
     }, 
    }) 
     .then(handleErrors) 
     .then(response => response.json()) 
     .then(data => { 
     dispatch({type: RECEIVE_LOGIN_TOKEN, payload: data}) 
     saveJwt(data) 
     }) 
     .catch(error => { 
     clearJwt() 
     dispatch({type: ERROR_LOGIN_TOKEN, payload: error.message}) 
     }) 
    addTask(task) 
    return task 
    } 

然後我在加不到「saveJwt(數據)」的代碼塊:

if (!confirmSelectDataExistance()) { 
     dispatch({ type: REQUEST_SELECT_DATA }) 
     const token = getJwt() 
     const headers = new Headers({ 
     'Authorization': `Bearer ${token}` 
     }) 
     const retrieveSelectData = fetch('/api/SelectData/SelectData', { 
     method: 'GET', 
     headers: { 
      'Content-Type': 'application/json;charset=UTF-8' 
     }, 
     }) 
     .then(handleErrors) 
     .then(response => response.json()) 
     .then(selectData => { 
      dispatch({ type: RECEIVE_SELECT_DATA, payload: selectData }) 
      saveSelectData(selectData) 
     }) 
    } 

所有它該做的是檢查項目是否在本地存儲中,如果是這樣,則在添加了JWT的情況下執行另一次獲取。

這裏是已經完成的動作,所以你可以看到它位於:

export const requestLoginToken = (username, password) => 
    (dispatch, getState) => { 
    dispatch({ type: REQUEST_LOGIN_TOKEN, payload: username }) 

    const payload = { 
     userName: username, 
     password: password, 
    } 

    const task = fetch('/api/jwt', { 
     method: 'POST', 
     body: JSON.stringify(payload), 
     headers: { 
     'Content-Type': 'application/json;charset=UTF-8' 
     }, 
    }) 
     .then(handleErrors) 
     .then(response => response.json()) 
     .then(data => { 
     dispatch({ type: RECEIVE_LOGIN_TOKEN, payload: data }) 
     saveJwt(data) 
     //selectData download if nothing is local storage. 
     if (!confirmSelectDataExistance()) { 
      dispatch({ type: REQUEST_SELECT_DATA }) 
      const token = getJwt() 
      const headers = new Headers({ 
      'Authorization': `Bearer ${token}` 
      }) 
      const retrieveSelectData = fetch('/api/SelectData/SelectData', { 
      method: 'GET', 
      headers: { 
       'Content-Type': 'application/json;charset=UTF-8' 
      }, 
      }) 
      .then(handleErrors) 
      .then(response => response.json()) 
      .then(selectData => { 
       dispatch({ type: RECEIVE_SELECT_DATA, payload: selectData }) 
       saveSelectData(selectData) 
      }) 
     } 
     }) 
     .catch(error => { 
     clearJwt() 
     dispatch({ type: ERROR_LOGIN_TOKEN, payload: error.message }) 
     }) 
    addTask(task) 
    return task 
    } 

所以現在我的第一次提取一個未定義的承諾結束了,它只是跳到下到谷底未定義任務的價值,,

你是否相信昨天它的工作完美,當我今天嘗試它的失敗。

爲什麼代碼塊導致整個事情失敗。我把它註釋掉,它工作正常..

我懷疑其有兩個承諾等做的,但必須有一個正確的方式來管理這個..我會認爲如果它正在保存的JWT令牌,這將表明它完成了第一次下載,但其他的錯誤......在該代碼塊或更可能的方式使用。

+0

待辦事項

export const requestLoginToken = (username, password) => (dispatch, getState) => { dispatch({ type: REQUEST_LOGIN_TOKEN, payload: username }) const payload = { userName: username, password: password, } const task = fetch('/api/jwt', { method: 'POST', body: JSON.stringify(payload), headers: { 'Content-Type': 'application/json;charset=UTF-8' }, }) .then(handleErrors) .then(response => response.json()) .then(data => { dispatch({ type: RECEIVE_LOGIN_TOKEN, payload: data }) saveJwt(data) //selectData download if nothing is local storage. return confirmSelectDataExistance().then(isConfirmed => { if (!isConfirmed) { dispatch({ type: REQUEST_SELECT_DATA }) const token = getJwt() const headers = new Headers({ 'Authorization': 'Bearer ${token}' }) const retrieveSelectData = fetch('/api/SelectData/SelectData', { method: 'GET', headers: { 'Content-Type': 'application/json;charset=UTF-8' }, }) .then(handleErrors) .then(response => response.json()) .then(selectData => { dispatch({ type: RECEIVE_SELECT_DATA, payload: selectData }) saveSelectData(selectData) }); return retrieveSelectData; } }) }) .catch(error => { clearJwt() dispatch({ type: ERROR_LOGIN_TOKEN, payload: error.message }) }) addTask(task) return task } 

你的意思是promise'task'在第一種情況下返回一些東西,或者在第二種情況下返回未定義的值(在更新後)? 我不知道我理解你的問題,但我懷疑從相應的函數返回retrieveSelectData可能是一個解決方案 – Igor

+0

想知道如果你可以擴大這個,如果你可以.. @Igor – si2030

+1

同樣,我不知道什麼是你的問題,但一般來說,如果您期望在requestSelectData解析它之後請求LoginToken解析的任何地方,則需要將後者鏈接到先前的鏈接。 它現在實現的方式promiseSelectData執行的類似於'fire and forget',唯一讓你意識到它完成的是action RECEIVE_SELECT_DATA – Igor

回答

1

假設confirmSelectDataExistance是異步和回報承諾和承諾retrieveSelectData必須向requestLoginToken解決方案之前解決可能是:如果saveSelectData也是異步你應該調用,而不是

return saveSelectData(selectData);