简体   繁体   中英

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.

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. 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. 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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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