【问题标题】:error to read data with redux and reactJs使用 redux 和 reactJs 读取数据时出错
【发布时间】:2019-05-02 16:22:47
【问题描述】:

我的代码有问题,我使用 reactJs 和 redux。

我的错误: TypeError: demo.map 不是函数

我的行动:

// DEMO
export const demoFetchRequest = () => {
  return {
    type: DEMO_FETCH_REQUEST,
  };
};
export const demoFetchSuccess = (demo) => {
  return {
    type: DEMO_FETCH_SUCCESS,
    demo,
  };
};
export const demoFetchError = (error) => {
  return {
    type: DEMO_FETCH_ERROR,
    error,
  };
};

我的减速机:

const initialState = {
  loading: false,
  demo: [],
  error: null,
};
const demo = (state = initialState, action) => {
  switch (action.type) {
    case DEMO_FETCH_REQUEST:
      return { ...state, loading: true };
    case DEMO_FETCH_SUCCESS:
      return { ...state, loading: false, demo: action.demo };
    case DEMO_FETCH_ERROR:
      return { ...state, loading: false, error: action.error };
    default:
      return state;
  }
};

我的数据:

const demoCompletedData = [
  {
    id: 1,
    marketId: "1111-1111-1111-1111",
    title: "Autonomous car",
    picture: "https://th?id=OIP.fdvfdvfdvfdvfd&pid=Api",
    language: "English",
    flag: "????????",
    completed: true,
    date: "22/01/2019 10:30",
    rate: 9.1,
    categories: {
      de: 1,
      sp: 2,
      uk: 0,
      fr: 1,
      us: 4,
    },
  },
  {
    id: 2,

我的组件DidMount:

componentDidMount() {
    this.fetchDemo();
    this.fetchUser();
  }

我前面的抓取:

 fetchDemo() {
    this.props.demoFetchRequest();
    const { marketId } = this.props.match.params;
    axios.get(`/api/v1/passport-authenticate/market/${marketId}`)
      .then((res) => { return res.data; })
      .then(demo => this.props.demoFetchSuccess(demo))
      .catch(error => this.props.demoFetchError(error));
  }

我的回报:

const { demo } = this.props;

我的渲染

<h5>{demo.title}</h5>
          <p>
            Demo
            {' '}
            {demo.flag}
          </p>
          {demo.map((el) => {
            return (
              <div>
                {Object.keys(el.categories).map((key) => {
                  return (
                    <p>
                      {key}
                      :
                      {el.categories[key]}
                    </p>
                  );
                })}
              </div>
            );
          })}

当我更改 Object.keys() 时出现另一个错误 TypeError: can't convert undefined to object

{Object.keys(demo).map((el) => {
            return (
              <div>
                {Object.keys(el.categories).map((key) => {
                  return (
                    <p>
                      {key}
                      :
                      {el.categories[key]}
                    </p>
                  );
                })}
              </div>
            );
          })}

你能帮我吗?

【问题讨论】:

  • 除了您的问题之外,我认为使用redux-thunk 并将获取逻辑移动到您的动作创建者会更好。您可以在一个函数中触发不同的操作(如 fetchRequest、fetch 本身)。
  • @devserkan 与 thunkMiddleware ?类似于带有 fetch 的 demo.service 和带有 dispatch 的 demo.action 之类的东西?
  • 是的,redux-thunkredux 使用异步操作的中间件,如果您的意思是“thunkMiddleware”的话。
  • 啊,如果这些答案解决了您的问题,请不要犹豫,接受您的问题的答案。
  • 我不知道你所说的“服务”和“动作”是什么意思,但这里有一个简单的例子来说明你是如何做到的:codesandbox.io/s/yk519xy7px 查找auth 动作。跨度>

标签: javascript reactjs redux


【解决方案1】:

Map 是 Array 原型上的一个方法。

代码 demo.map 仅在 demo 是一个数组时才有效,但在您的代码中它似乎是一个对象。

看起来您的意思是您的渲染方法略有不同,可能是:

<h5>{demo.title}</h5>
          <p>
            Demo
            {' '}
            {demo.flag}
          </p>
          {demo.categories.map((el) => {
            return (
              <div>              
                    <p>                      
                      {el}
                    </p>
              </div>
            );
          })}

我也刚刚注意到您的类别是一个对象,而不是一个数组,因此您的渲染方法中处理类别的部分应该是这样的:

{demo.categories && Object.keys(demo.categories).map((el) => {
            return (
              <div>              
                    <p>                     
                      {el.categories[key]}
                    </p>            
              </div>
            );
          })}

以上有几点需要注意:

  1. 如果您的数据实际上是一个演示数组,而不是单个演示对象,那么您需要映射该演示对象数组。您调用 demo.title 的方式似乎表明情况可能并非如此?
  2. 我们检查 demo.categories 确实存在。
  3. 对类别对象执行 Object.keys 后,我们就不再需要 object.keys - 类别对象的属性只是字符串。

【讨论】:

  • 感谢您的帮助,但我有一个错误:demo.categories is undefined
  • @user11429164 如果类别对象可能未定义,那么您需要添加一些代码来检查它是否存在。
  • 正如您在上一个问题中看到的那样,您应该首先映射传入的数据,这就是@DavidHall 指出的demo 上不能使用map 的原因。此外,正如他所说,您需要检查您的数据。你有一个loading 状态,使用它。这就是你保留它的原因。有条件地渲染您的数据,如果 loading 为 true,则渲染其他内容(微调器等),然后在 fetch 完成后渲染您的数据。
  • @devserkan 谢谢,没关系,我可以读取对象“类别”的数据,现在我尝试使用条件渲染来渲染我的数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-30
  • 2022-08-14
  • 2014-05-08
  • 2017-09-13
  • 1970-01-01
相关资源
最近更新 更多