Reactjs 通过axios使用redux observable

Reactjs 通过axios使用redux observable,reactjs,redux,redux-observable,Reactjs,Redux,Redux Observable,你好 我试图通过epic更新我的数据库,然后将响应发送回更新存储。 但是,虽然该操作最终发送到我的存储,但有效负载是未定义的。我知道updateArticle获取速度不够快是有问题的。我如何允许它在发送到商店之前等待updateArticle返回响应 我的史诗: action$.pipe( flowing left-to-right, calling each function with the output of the last one. filter(action =>

你好

我试图通过epic更新我的数据库,然后将响应发送回更新存储。 但是,虽然该操作最终发送到我的存储,但有效负载是未定义的。我知道updateArticle获取速度不够快是有问题的。我如何允许它在发送到商店之前等待updateArticle返回响应

我的史诗:

  action$.pipe(
flowing left-to-right, calling each function with the output of the last one.
    filter(action => action.type === "ADD_ARTICLE_EPIC"), 
    switchMap(
      action =>
        from(updateArticle(action.payload)).pipe(
          map(action => {
            console.log(action);
            return { type: "ADD_ARTICLE", payload: action.payload };
          })
        )
      // return { type: "ADD_ARTICLE", payload: action.payload };
    )
  );
我的axios:

export const updateArticle = async article => {
  const response = await axios.post(`/articles/updateArticle`, article);
  return response.data;
};

首先,您可以使用from和promises,所以我认为在updateArticle中不需要使用wait。而且,在map中,您正在从action访问有效负载,但该函数范围内的action已经是来自请求的响应。试试这个:

action$.pipe(
  filter(action => action.type === "ADD_ARTICLE_EPIC"), 
  switchMap(
    action =>
      from(updateArticle(action.payload)).pipe(
        map(response => {
          console.log(response);
          return { type: "ADD_ARTICLE", payload: response.data };
        })
      )
  )
);
然后为您服务:

export const updateArticle = article => {
  return axios.post(`/articles/updateArticle`, article);
};

啊,真漂亮。非常感谢你。它完美地工作了!