2016-12-15 85 views
4

我收到错误“Redux操作必须是普通对象,使用自定义中间件进行异步操作。”用下面的代码。Redux Actions必须是普通对象。使用自定义中间件进行异步操作

export const getFriends =() => async(): Action => { 

    const requestConfig = { 
    httpMethod: 'GET', 
    version: 'v2.7', 
    }; 
    let hasMore = false; 
    let friends = []; 

    do { 
    const infoRequest = new GraphRequest(
    '/me/friends', 
    requestConfig, 
    (error, result) => { 
     if (error) { 
     alert('Error fetching data facebook friends'); 
     } else { 
     result.data.forEach((value) => { 
      friends.push(value) 
     }); 
     if (result.paging && result.paging.next) { 
      hasMore = true; 
      requestConfig.parameters.after = result.paging.cursors.after; 
     } else { 
      hasMore = false; 
     } 
     } 
    }); 
    await new GraphRequestManager().addRequest(infoRequest).start(); 
    } while (hasMore); 
    return { 
    type: 'GET_FRIENDS', 
    payload: friends // this is empty because there is no await on GraphAPI 
    }; 
}; 

如果我删除了异步和等待,朋友返回是空的,因为函数调用返回之前GraphAPI调用返回

export const getFriends =() =>(): Action => { 

    const requestConfig = { 
    httpMethod: 'GET', 
    version: 'v2.7', 
    }; 
    let hasMore = false; 
    let friends = []; 

    do { 
    const infoRequest = new GraphRequest(
    '/me/friends', 
    requestConfig, 
    (error, result) => { 
     if (error) { 
     alert('Error fetching data facebook friends'); 
     } else { 
     result.data.forEach((value) => { 
      friends.push(value) 
     }); 
     if (result.paging && result.paging.next) { 
      hasMore = true; 
      requestConfig.parameters.after = result.paging.cursors.after; 
     } else { 
      hasMore = false; 
     } 
     } 
    }); 
    new GraphRequestManager().addRequest(infoRequest).start(); 
    } while (hasMore); 
    return { 
    type: 'GET_FRIENDS', 
    payload: friends // this is empty because there is no await on GraphAPI 
    }; 
}; 

回答

1

的错误意味着什么这听起来像。 Redux期待一个简单的对象。我看到标签为redux-thunk的问题。如果您正在使用redux-thunk,也许您没有正确设置中间件的商店。如果你没有使用redux-thunk,那么我会推荐它作为调度异步操作的前往库。

这会给你很好的洞察关于调度不是普通对象的redux动作。 How to dispatch a Redux action with a timeout?

编辑:你错过了实际的调度,并试图简单地返回普通的对象...需要返回一个调度...像下面的东西(没有测试,但应该让你关闭):

export const getFriends =() => { 
     return (dispatch) => { 
     const requestConfig = { 
      httpMethod: 'GET', 
      version: 'v2.7', 
     }; 
     let hasMore = false; 
     let friends = []; 

     do { 
      const infoRequest = new GraphRequest(
      '/me/friends', 
      requestConfig, 
      (error, result) => { 
       if (error) { 
       alert('Error fetching data facebook friends'); 
       } else { 
       result.data.forEach((value) => { 
        friends.push(value) 
       }); 
       if (result.paging && result.paging.next) { 
        hasMore = true; 
        requestConfig.parameters.after = result.paging.cursors.after; 
       } else { 
        hasMore = false; 
        return dispatch({ 
        type: 'GET_FRIENDS', 
        payload: friends    
        }); 
       } 
       } 
      }); 
      await new GraphRequestManager().addRequest(infoRequest).start(); 
     } while (hasMore); 
     } 
    }; 
+0

是的,我正在使用redux-thunk。看到我做的同时。我需要等待,因为这样做。 GraphAPI不会一次返回整个回报,所以我必须分页。我怎么做? – user43286

+0

为了跟上该代码片段,redux-thunk非常棒,因为您可以从同一个动作分派多个事件。因此,您可以派发事件来指示加载过程的开始,然后在每次接收数据时调度ADD_FRIENDS方法,而不是等到结束。不需要返回任何东西,只需要在需要时实际调度()简单对象操作 –

+0

但是,while循环退出。 – user43286

相关问题