![](/img/trans.png)
[英]How to fix “TypeError: categories.map is not a function” error in React
[英]TypeError: undefined is not a function (evaluating 'categories.map')
嗨,我有這樣的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數組時,代碼運行良好,並且我獲得菜單列表nbut,但是當我添加子類別時,出現以下錯誤:TypeError:TypeError:undefined不是一個函數(評估'categories.map')
Map bloc顯示json數據之前的Console.log(this.props.categories),我也嘗試返回category.map((obj)=> ...,但也沒有用
對於任何有這樣問題的人,請在您的json文件中檢查逗號。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.