【问题标题】:TypeError: undefined is not a function (evaluating 'categories.map')TypeError:未定义不是函数(评估'categories.map')
【发布时间】:2018-08-04 01:29:44
【问题描述】:

嗨,我有这样的 json 文件

[
  {
    "id": 1,
    "code":"films",
    "name":"FILMS",
    "icon":"film",
    "image":"http://static.tvmaze.com/uploads/images/large_landscape/137/343398.jpg",
    "subs": [
      {
        "id": "1-1",
        "code": "553",
        "name": "films 1",
      },
      {
        "id": "1-2",
        "code": "554",
        "name": "film 2",
      }
    ]
  },
  {
    "id": 2,
    "code":"series",
    "name":"SERIES",
    "icon":"television",
    "image":"http://static.tvmaze.com/uploads/images/large_landscape/142/356297.jpg",
    "subs": [
      {
        "id": "2-1",
        "code": "555",
        "name": "series 1",
      },
      {
        "id": "2-2",
        "code": "556",
        "name": "series 2",
      }
    ]
  }
]

我正在使用 redux (axios) 获取它

// CATEGORIES
export function retrieveCategoriesSuccess(res) {
    return {
        type: types.RETRIEVE_MOVIES_CATEGORIES_SUCCESS,
        categories: res.data
    };
}
export function retrieveMoviesCategories() {
    return function (dispatch) {

        return axios.get(FETCHING_MOVIES_URL + "data/categories.json")
        .then(res => {
            dispatch(retrieveCategoriesSuccess(res));
        })
        .catch(error => {
            console.log("Categories: ", error); //eslint-disable-line
        });
    };
}

我尝试以这种方式将其映射到我的菜单:

    //return categories.map((obj) => (
    return this.props.categories.map((obj, key) => {
        <View key={obj.id}>
            <Text style={styles.text}>{obj.name}</Text>
        </View>
        {_renderSubMenu(obj)}
    })

当我删除 subs 数组时,代码效果很好,我得到了菜单列表,但是当我添加子类别时,我得到了这个错误: TypeError: TypeError: undefined is not a function (evalating 'categories.map')

Console.log(this.props.categories) 前map bloc显示json数据, 我也试过 return categories.map((obj) =>... 但也没有用

【问题讨论】:

  • console.log(this.props.categories)的输出是不是和json一模一样?更具体地说,一个数组?
  • 谢谢我再次检查,发现问题在于 subs 对象中的尾随逗号

标签: javascript react-native redux axios redux-thunk


【解决方案1】:

对于遇到此类问题的任何人,请检查 json 文件中的尾随逗号。

【讨论】:

    猜你喜欢
    • 2014-01-01
    • 2015-03-30
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 2014-09-13
    • 2018-08-27
    相关资源
    最近更新 更多