javascript - 修复 Redux 有效负载正在控制台中登录减速器,但在模式中说无法读取 null 的属性?
问题描述
想知道你们是否可以帮我解决这个问题:
在我的行动中,我有:
export const getProduct = (id) => (dispatch) => {
dispatch(setItemsLoading());
axios.get(`http://localhost:5000/api/products/view/${id}`).then((res) =>
dispatch({
type: GET_ITEM,
payload: res.data,
})
);
};
在减速机中:
case GET_ITEM:
console.log(action.payload);
return {
...state,
/* items: action.payload, */
singleItem: action.payload,
};
控制台记录了我得到但在我的模型中的对象:
static propTypes = {
isAuthenticated: PropTypes.bool,
singleItem: PropTypes.object.isRequired
}
componentDidMount() {
this.props.getProduct('5f4589e4b7b3aa98e4ca79ab')
}
<Label for="name">{this.props.item.name}</Label>
<Input type="text" name="name" id="name" placeholder="Product Name" onChange={this.onChange} />
<Label for="description">{this.props.item.description}</Label>
const mapStateToProps = state => ({
item: state.item.singleItem,
user: state.auth,
isAuthenticated: state.auth.isAuthenticated
})
还有更多,但这已经足够了,我不断得到:
TypeError: Cannot read property 'name' of null
ProductModal.render
src/components/products/ProductModal.js:65
62 | <ModalBody>
63 | <Form onSubmit={this.onSubmit}>
64 | <FormGroup>
> 65 | <Label for="name">{this.props.item.name}</Label>
| ^ 66 | <Input type="text" name="name" id="name" placeholder="Product Name" onChange={this.onChange} />
67 | <Label for="description">{this.props.item.description}</Label>
68 | <Input type="textarea" name="description" id="item" placeholder="description" onChange={this.onChange} />
我的有效载荷是:
_id(pin):"5f4589e4b7b3aa98e4ca79ab"
name(pin):"Cajun"
description(pin):"Mouth watering artisan freshly made beef jerky, made with love in Texas! - 8 OZ bag"
creator(pin):"Jenn & James"
imgUrl(pin):"http://dgegergerg.dev/masonsbeef/beef1.jpg"
quantity(pin):20
__v(pin):0
它是一个对象
解决方案
推荐阅读
- python - Python 四位密码查找器
- r - (R) 错误:未使用的参数(禁止 = 表达式(x2 > x1))
- php - 如何使用 PHP 开发工具包创建具有对 S3 的完全访问权限的编程访问权限的 IAM 用户
- javascript - 我在使用 Javascript 和 PHP 将函数分配给 SweetAlert 时出错
- email - MsGraph WebAPI 访问共享邮箱
- urlencode - Rest Sharp 转义 %5F 字符
- database - 为什么在区块链中查询(即读取数据)很慢?
- python - 如何计算 Django 中客户的余额?
- node.js - 如何在一个会话中保存身份验证以使用 AdonisJS 5 在其他页面中检索?
- android - 如何使用表情符号作为可绘制对象来显示五彩纸屑?