简体   繁体   中英

TypeError: undefined is not a function (evaluating 'categories.map')

Hi i have this json file like this

[
  {
    "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",
      }
    ]
  }
]

I am fetching it using 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
        });
    };
}

I try to map it to my menu this way:

    //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)}
    })

when i remove the subs array the code works great and i get the menu list nbut when i add the subcategories i get this error: TypeError: TypeError: undefined is not a function (evaluating 'categories.map')

Console.log (this.props.categories) before the map bloc show the json data, i tried also return categories.map((obj) =>... but didn't work either

对于任何有这样问题的人,请在您的json文件中检查逗号。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM