我正在尝试从我的jobs对象中获取位置字段,但是当我将location.address
放在例如js中语法错误时。
这是我的常量,地址,城市和邮政编码的一切都很好。
const { title, avatar, company, postDate, jobType, payment, duration, description, address, city, postcode } = this.props.navigation.state.params;
这是我的数据库计划
jobs: [{
title: {type: String},
company: {type: String},
avatar: {type: String},
created: {type: Boolean},
applied: {type: Boolean},
description: {type: String},
location: [{
address: {type: String},
postcode: {type: String},
city: {type: String}
}],
jobType: {type: String},
payment: {type: String},
duration: {type: String},
postDate: {type: Date},
expDate: {type: Date}
}]
答案 0 :(得分:0)
location
是array
个对象。你必须使用一些循环来访问它的属性,如
location.forEach(e=>console.log(e.address))
这可能是你的表达
const { title, avatar, company, postDate, jobType, payment, duration, description, location } = this.props.navigation.state.params;
答案 1 :(得分:0)
你不能在数组上点。
您的地址嵌套在数组中。
location: [{
address: {type: String},
postcode: {type: String},
city: {type: String}
}]
要访问它,您需要像这样
对其进行索引jobs[0].location[0].address
答案 2 :(得分:0)
location是一个数组。据推测,如果每个数据的位置是单个的,则不必是数组。 取代
...
location: [{
address: {type: String},
postcode: {type: String},
city: {type: String}
}],
...
到
...
location: {
address: {type: String},
postcode: {type: String},
city: {type: String}
},
...
并简单地使用location.address
但它应该是一个数组,就像我们使用数组一样工作
第一元素
location[0].address
第二元素
location[1].address
对于所有元素
location.forEach(varName){
varName.address
}