简体   繁体   English

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

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

I am trying to get the location fields from my jobs object but when I put location.address for example it looks like the syntax is wrong in js. 我试图从我的工作对象中获取位置字段,但是例如当我放置location.address ,它似乎在js中语法错误。

This are my constants where everything works fine apart of address, city and postcode. 这是我的常量,除了地址,城市和邮政编码外,其他所有内容都可以正常工作。

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

And here is my DB scheme 这是我的数据库方案

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 is a array of objects there. location是那里的一array对象。 You'll have to use some loop to access properties of it like 您必须使用一些循环来访问它的属性,例如

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

This could be your expression 这可能是你的表情

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

You can't dot on an array. 您不能在数组上点。

Your address is nested inside of an array. 您的地址嵌套在数组内。

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

To access it you need to index on it like this 要访问它,您需要像这样对其进行索引

jobs[0].location[0].address

location is an array. location是一个数组。 Supposedly if location of each data is singleton, that is need not be array. 假设每个数据的位置都是单例的,则不必数组。 Replace 更换

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

to

...
location: {
          address: {type: String},
          postcode: {type: String},
          city: {type: String}
      },
...

and simply use location.address 只需使用location.address

but it is supposed to be an array, work with it like we work with array 但它应该是一个数组,像处理数组一样使用它

For 1st element 对于第一个元素

location[0].address

For 2st element 对于第二元素

location[1].address

For all elements 对于所有元素

location.forEach(varName){
varName.address
}

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

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