【问题标题】:Is it posible to send a post request from reactjs to expressjs and in express fetching data and sending that back to reactjs with redux?是否可以从 reactjs 向 expressjs 发送 post 请求并快速获取数据并使用 redux 将其发送回 reactjs?
【发布时间】:2021-08-17 01:38:10
【问题描述】:

我是 redux 的新手。

在我的场景中,我有一个输入字段,我使用这个输入字段来填充发布请求的正文:

Action ===> export const fetchSearchProducts = (search) =>  (dispatch) => {


   fetch("/search", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(search),
  })
    .then((res) => res.json())
    .then((data) => {
      dispatch({ type: FETCH_SEARCH_ITEM, payload: data }); ===> this errors out saying Unhandled Rejection (TypeError): dispatch is not a function


      // this console.log() actually returns the data im looking for
      console.log(data)
      
    });
  };

这篇文章发送到 ExpressJS 服务器,在那里我使用“req.body.search”来填充 API 获取的 url 字段。 "url = https://example.nl?search=${req.body.search};"

直到这里它似乎工作......

现在我的问题是我试图在动作中使用 redux dispatch 填充状态.....但我不断收到这个错误:Unhandled Rejection (TypeError): dispatch is not a function

【问题讨论】:

    标签: javascript reactjs express redux


    【解决方案1】:

    redux 本身不支持异步调度,需要一个中间件来实现。您可以添加任何允许异步调度的中间件,例如 redux-thunk 或类似的库。

    您可以查看official documentation 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      • 2018-04-23
      • 2019-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多