我在React Native和Redux方面还很新,我在使用POST方法时遇到了麻烦。
我想向API发送一些信息(关于菜肴),以便以后可以获取已发送的菜肴列表。 (get方法对我有用)。
我这样写:
export function addDishToFavourites(meal: Meal) {
return async (dispatch: any, getState: any) => {
let isDishInFavourites = getState().dish.isDishInFavourites;
var deviceId = 'goldfish';
var MealSetID = meal.ID;
req(dispatch, getState, [ACTION_TYPES.ADD_DISH_TO_FAVOURITES_LOADING, ACTION_TYPES.ADD_DISH_TO_FAVOURITES_SUCCESSED, ACTION_TYPES.ADD_DISH_TO_FAVOURITES_FAILED], `FavoritesUserDishes?UserDeviceID=${deviceId}&MealSetID=${MealSetID}`, []);
}
}
req函数返回一个api网址,我写的子网址也可以。我的减速器是:
case ACTION_TYPES.ADD_DISH_TO_FAVOURITES_LOADING:
return { ...state, isFetching: true };
case ACTION_TYPES.ADD_DISH_TO_FAVOURITES_SUCCESSED:
return { ...state, isFetching: false, favouriteDishes: action.payload };
case ACTION_TYPES.ADD_DISH_TO_FAVOURITES_FAILED:
return { ...state, isFetching: false, error: true, errorMessage: action.payload };
运行添加到收藏夹功能时出现错误: TypeError:无法读取未定义的属性“名称”
此错误位于: 在板中(在Component.tsx:116处)
该板块是可以在其他视图中正确渲染的组件。
Ionic中的方法如下:
addDishToFavorites() {
let body = {
"UserDeviceID": this.UserDeviceID,
"MealSetID": this.setPage.ID
}
this.storage.get('token').then((val) => {
this.token = val;
let headers = new Headers({ 'Authorization': '*******' + this.token, 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
this.http.post(this.apiUrl + "FavoritesUserDishes", body, options)
.subscribe(data => {
this.isDishInFavorites = !this.isDishInFavorites;
this.showAlertAddedToFavorites();
}, error => {
});
});
}
平板组件代码:https://pastebin.com/1KUtMdgh 和菜式https://imgur.com/a/0VHywjC
谢谢您的帮助:)