0

I want to get API information and set the data-table. But my code has error. Error messages are below in console.

Property or method "items" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.

Please someone advise me?

and also my English is not good so please someone help my question to correctly English.

<template>
  <v-app>
    <v-data-table dense :headers="headers" :item="items" class="elevation-1"></v-data-table>
  </v-app>
</template>

<script lang="ts">
import { Component, Vue } from "nuxt-property-decorator";
import axios from "axios";

@Component
export default class AxiosPractice extends Vue {
  items: object[] = [];

  headers = [
    { text: "id", value: "postId" },
    { text: "name", value: "name" },
    { text: "email", value: "email" }
  ];
  async mounted() {
    const response = await axios.get(
      "https://jsonplaceholder.typicode.com/posts/1/comments"
    );
    this.itmes = response.data.map((comment:any) => ({
      postId: comment.postId,
      email: comment.email,
      name: comment.name
    }));
  }
}
</script>
1
  • Try not to mix await with then(), it just makes things confusing Commented Jul 17, 2020 at 7:42

1 Answer 1

3

I suppose that response is an object that contains the property data (see docs) which (in this case) holds an array of objects. Assuming you want to save postId, email and name for every comment in items, you could try this:

const response = await axios.get('https://jsonplaceholder.typicode.com/posts/1/comments');

this.items = response.data.map(comment => ({
  postId: comment.postId,
  email: comment.email,
  name: comment.name,
}));
Sign up to request clarification or add additional context in comments.

12 Comments

Thank you for your answer. I tried your code, but had error.
itmes : ⇒Property 'items' does not exist on type 'AxiosPractice'. Did you mean 'itmes'
comment⇒Parameter 'comment' implicitly has an 'any' type.
I really understand your explanations and i really appreciated it. Could you explain about error.please??
You have a typo in items: itmes:object.
|

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.