簡體   English   中英

動作必須是普通對象。 使用自定義中間件進行異步操作。 redux-thunk相關

[英]Actions must be plain objects. Use custom middleware for async actions. redux-thunk related

我是redux初學者。 我正在使用redux-thunk,但是,我從此函數中僅收到錯誤消息。

Error: Actions must be plain objects. Use custom middleware for async actions.
 this.props.sendPaypalOrderToFirebase(body)



export const sendPaypalOrderToFirebase = (orderInfo) => {
  async (dispatch, getState) => {
    database.ref('paypalOrders/' + uuid()).set({
      orderInfo
    });
    return dispatch(paypalOrderFirebaseSuccess(orderInfo))
  }
}

export const createOrder = (paymentMethod, paymentData) => ({
  type: actionTypes.CREATE_ORDER,
  paymentMethod,
  paymentData
});
export const paypalOrderFirebaseSuccess = (orderInfo) => ({
  type: actionTypes.PAYPAL_ORDER_FIREBASE_SUCCESS,
  orderInfo
})

謝謝你的幫助。

這里的問題是您對async的使用:

export const sendPaypalOrderToFirebase = (orderInfo) => {
  async (dispatch, getState) => {
    // ...
  }
}

該代碼段的實際作用是創建一個異步箭頭函數,該函數永遠不會被調用,因為它不是從動作創建者返回的(調用此動作創建者只會返回undefined )。

可以通過以下任一方法解決此問題:

  1. 添加return

     export const sendPaypalOrderToFirebase = (orderInfo) => { return async (dispatch, getState) => { // ... } } 
  2. 或刪除一些括號:

     export const sendPaypalOrderToFirebase = (orderInfo) => async (dispatch, getState) => { // ... } 

最后,值得考慮的是,您是否真的需要async功能,因為您似乎沒有在thunk塊中創建Promise (或至少await一個)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM