我的模型如下所示:
coverPhoto: {
type: String,
required: true,
},
images: [{
type: String
}],
location: {
address: { type: String, required: true },
city: { type: String, required: true },
postalCode: { type: Number, required: true },
country: { type: String, required: true }
},
我播种了一些数据,它在数据库中正确显示为:
"coverPhoto": "/images/SUSI.jpg",
"images": [
"/images/SUSI1.jpg",
"/images/SUSI2.jpg",
"/images/SUSI3.jpg"
],
"location": {
"address": "AM PICHELSSEE",
"city": "BERLIN",
"postalCode": 13595,
"country": "Germany"
},
现在尝试在前端访问它:
<Col>
<Image src={renting.coverPhoto} alt={renting.name} fluid />
</Col>
<Col>
<Image src={renting.images[0]} alt={renting.name} fluid />
</Col>
我添加了coverPhoto 以供参考,因为一切正常。但是当我尝试访问 images[0], 1 ,[2] 时,我收到此错误:
编辑:我为上面的输入显示了错误的错误。
错误是:无法读取未定义的属性 0。
当我尝试访问 {renting.location.address} 时,我收到与“无法读取未定义的属性“地址”相同的错误”
当我在rentingActions 中控制台登录时,一切都很好。当我 console.log(data) 时,我得到了正确的输出。
console.log(data.images[0]) ==> /images/SUSI1.jpg console.log(data.location.address) ==> AM PICHELSSEE
所以我想我的问题是,为什么我不能访问嵌套对象?是语法错误吗?链接到我的 代码框