1

User.vue

<template>
  <div>
    <!-- <div class="cont-color">{{ user.name }} {{ user.age }}</div> -->

    <div v-for="(item, key) in user" :key="key">
      {{ item }}
    </div>
  </div>
</template>

<script>
import { datalisttwo } from "./datalisttwo";
export default {
  name: "User",
  data() {
    return {
      lists: datalisttwo,
    };
  },
  computed: {
    user: function () {
      return this.lists.find((item) => item.cmd_id === this.$route.params.id);
    },
  },
};
</script>

Working code:- https://codesandbox.io/s/hardcore-cartwright-vi6qg?file=/src/components/User.vue

How to call specific value from an array in Vuejs.

At present, It is calling all values, from an array. But want to call only {{name}} and {{age}} values.

If i remove the v-for and write directly like this {{ user.name }} {{ user.age }} --> It is printing the array values, But getting error as

[Vue warn]: Error in render: "TypeError: Cannot read properties of undefined (reading 'name')" TypeError: Cannot read properties of undefined (reading 'name')

3
  • What does call the url query params correctly for both arrays means? Whats is the current issue? Commented Feb 10, 2022 at 9:36
  • For my api endpoint url, i need to pass the query params. As stated above. for both arrays Commented Feb 10, 2022 at 9:41
  • At present issue is,Unable to call the query params dynamically. if i click on router-link, in the left hand side, i am unable to call the exact query params as stated above. and for right hand side, call query params by /id Commented Feb 10, 2022 at 9:46

3 Answers 3

1

I think problem is in your computed block, use below solution

computed: {
    user: function () {
      return this.lists.find((item) => item.cmd_id === this.$route.params.id);
    },
  },

sandbox

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, With your solution, i have made few changes. here it is codesandbox.io/s/keen-yonath-50n77?file=/src/components/…
If you can see in the url section it is now showing as, HelloWorld?pm_id=1 instead can i make like this, by writing / after HelloWorld. HelloWorld/?pm_id=1 then it gives correct url for me
Also for datalisttwo array, can i call query params like.... /id for getting values? please
HelloWorld/?pm_id=1 , its not convention,
For datalisttwo array. I have API like this 35.162.202.237:3000/somename/id So in frontend i need to show like by making api call, on click of each routerlink. display values
|
1

In User.vue you have this comparison:

return this.lists.find((item) => item.id === this.$route.params.id);

but the data you use from datalisttwo.js does not have the property id but instead cmd_id

So that find result will always be undefined which is the error you see in the javascript console:

[Vue warn]: Error in render: "TypeError: Cannot read properties of undefined (reading 'name')"

so you'll need to update your find to be the following:

return this.lists.find((item) => item.cmd_id === this.$route.params.id);

3 Comments

If you can see in the url section it is now showing as, HelloWorld?pm_id=1 instead can i make like this, by writing / after HelloWorld. HelloWorld/?pm_id=1 then it gives correct url for me. And for datalisttwo array, query params are not calling, like User/id for getting values?
just for reference, /?YourQueryParams=SomeValue is the same as ?YourQueryParams=SomeValue they are both valid urls which will have the same effect
After making above changes, in the console i am getting some errors like [Vue warn]: Error in render: "TypeError: Cannot read properties of undefined (reading 'name')" TypeError: Cannot read properties of undefined (reading 'name') codesandbox.io/s/hardcore-cartwright-vi6qg?file=/src/components/…
1

I guess you need to return an Array on that computed property. Use filter instead of find.

 computed: {
    user: function () {
      return this.lists.filter((item) => {
        if(item.cmd_id === this.$route.params.id) {
           return item
        }
      })
    }        
 }

Comments

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.