现在,我的API中有一个类似于以下内容的对象-
design: {
"_id": "5b291367e3d1390d86931b44",
"name": "PRP15",
"thickness1": {
"name": "4/4",
"price": 12.64
},
"thickness2": {
"name": "5/4",
"price": 14.99
},
"createdAt": "2018-06-19T14:29:59.930Z",
"updatedAt": "2018-07-22T01:30:16.316Z",
"__v": 0,
"photo": null,
}
当我控制台登录 design.thickness1 我要返回
{name: "4/4", price: 12.64}
但是,当我执行 design.thickness1.price 它返回时
undefined
如果我执行 design.thickness2.price
,它也会这样做我不知道为什么,而且即时通讯似乎无济于事。任何帮助将非常感激!我什至尝试用相同的对象创建变量,并且确实返回正确的值
编辑:这是我目前的使用方式
const { design } = this.props
console.log('Design: ' + JSON.stringify(design, null, 2));
//returning
//{
//"_id": "5b291367e3d1390d86931b44",
//"name": "PRP15",
//"thickness1": {
//"name": "4/4",
//"price": 12.64
//},
//"thickness2": {
//"name": "5/4",
//"price": 14.99
//},
//"createdAt": "2018-06-19T14:29:59.930Z",
//"updatedAt": "2018-07-22T01:30:16.316Z",
//"__v": 0,
//"photo": null,
//}
console.log('Thickness1 Price: ' + JSON.stringify(design.thickness1, null, 2));
//returning {name: "4/4", price: 12.64}
console.log('Design Price: ' + JSON.stringify(design.thickness1.price, null, 2));
//returning undefined
const mapStateToProps = (state) => ({
design: state.part_list.designs
});
export default connect(mapStateToProps, mapDispatchToProps)(DoorOrders);
答案 0 :(得分:1)
固定
基本上反应是试图在它具有值之前将其渲染并崩溃,因此我添加了条件
if(!designs[0].thickness1){
return null
}else{
console.log('Design Price: ' + (JSON.stringify(designs[0].thickness1.price)));
console.log(typeof (designs[0].thickness1.price));
}