0

I am new to vue and can't find a solution to this -

I have a JSON object here, and I am trying to dynamically fetch the "info" of a user based on their "userRegion".

{
"userData": {
    "kr": {
      "info": {
        "name": "testing-123",
     }
    },
    "any": null,
    "us": null,
    "eu": {
      "info": {
        "name": "testing-456",
     }
    },
  },
   "userRegion": "eu" 
}

I then have this object in vue and I want to dynamically change region and pull the data from the object based on this region value in the "user" object below.

 user:{ 
        region:  this.userData.userRegion,
        name: this.userData[this.user.region].info.name
    },

For example, I have tried using something like this

this.userData.userData[this.user.region] 

but I get an error:

TypeError: Cannot read property 'region' of undefined"

the variable I am using "userData" is passed down from the parent like so:

<infoWindow :userData='userData'></infoWindow>

and is set as a prop:

props: {
        userData: app.userData,
     },

any help would be aprpeciated, thanks

1 Answer 1

1

I don’t really understand where you are setting this user, whether its part of an initialized data object, or a computed property. However, there is a temporal issue there:

user: {
    region: this.userData.userRegion,
    name: this.userData[this.user.region].info.name
},

In order to set up user.name, user.region needs to be already there. But since you are creating the user object at once, this does not work. So you either have to split that up, or repeat the logic for the user region again:

user: {
    region: this.userData.userRegion,
    name: this.userData[this.userData.userRegion].info.name
},
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, forgot to explain it was from an api that returns the object! This worked perfectly though - thank you! And thanks for the explanation, it makes so much more sense now :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.