繁体   English   中英

当一个字符串在另一个字符串中时,如何从DB获取数据?

[英]How to get data from DB when a string is in another string?

我试图从我的工作对象中获取位置字段,但是例如当我放置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}
  }]

location是那里的一array对象。 您必须使用一些循环来访问它的属性,例如

location.forEach(e=>console.log(e.address))

这可能是你的表情

const { title, avatar, company, postDate, jobType, payment, duration, description, location } = this.props.navigation.state.params; 

您不能在数组上点。

您的地址嵌套在数组内。

  location: [{
      address: {type: String},
      postcode: {type: String},
      city: {type: String}
  }]

要访问它,您需要像这样对其进行索引

jobs[0].location[0].address

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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM