我有一个AddtoCart功能,在将产品添加到购物车时,单击时引发未定义的错误。
该函数index.js中的错误
componentWillUpdate(nextProps, nextState) {
if (nextState.cartPopup && nextProps.reducerKeyProductCartPage !== this.props.reducerKeyProductCartPage && nextProps.reducerKeyProductCartPage) {
if (nextProps.reducerKeyProductCartPage.status && nextProps.reducerKeyProductCartPage.status[0].toLowerCase().includes("error")) {
this.notify(nextProps.reducerKeyProductCartPage.message[0]);
}
else {
this.notify(nextProps.reducerKeyProductCartPage.message[0], "success");
}
this.setState({ cartPopup: false });
}
}
以下是“添加到购物车”功能:
addToCart = (data) => {
this.setState({ cartPopup: true, deleteCart: false });
let customerId = cookie.load("customerId") ? cookie.load("customerId") : 0;
let getUrl = '';
let stateProductData = {}
if (customerId) {
getUrl = constant.BaseUrl + this.state.languageValue + "-" + this.state.storeValue + "/rest/V1/customer/addtocart";
stateProductData = { "customer_id": customerId, "lang": this.state.languageValue + "_" + this.state.storeValue, "product_id": data, "quantity": 1 }
}
else {
getUrl = constant.BaseUrl + this.state.languageValue + "-" + this.state.storeValue + "/rest/V1/customer/guestaddtocart";
stateProductData = { "quote_id": cookie.load("cartValue"), "lang": this.state.languageValue + "_" + this.state.storeValue, "product_id": data, "quantity": 1 }
}
this.props.commonGateway({
reqData: stateProductData,
url: getUrl, method: 'POST',
resource: "cartProduct", reducerKey: "reducerKeyProductCartPage"
});
this.props.commonGatewayRequestDone({ reducerKey: "reducerKeyProductCartStatus" });
}
以下是渲染方法,该方法具有添加到其他组件中的购物车按钮:
return (
<div>
<MainContainer block={idx + 2} fetchData={this.fetchData} data={this.props["reducerKeyFetchLandingPage" + (idx + 2)] ? this.props["reducerKeyFetchLandingPage" + (idx + 2)] : ""} addToCart={this.addToCart} addToWishList={this.addToWishList} rtl={this.state.languageValue} />
</div>
);
的屏幕截图