paolomax

Untitled

Dec 9th, 2021
648
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template>
  2.   <div id="my-data">
  3.     <template v-if="showData">
  4.       <b-table striped hover :items="myData"></b-table>
  5.     </template>
  6.     {{variabileDiTest}}
  7.   </div>
  8. </template>
  9.  
  10. <script>
  11. import axios from "axios";
  12.  
  13. export default {
  14.   name: "MyData",
  15.   props: ["variabileDiTest"],
  16.   data() {
  17.     return {
  18.       myToken: localStorage.getItem("accessToken"),
  19.       showData: false,
  20.       myData: undefined,
  21.       filteredData: {},
  22.     };
  23.   },
  24.   async mounted() {
  25.     try {
  26.       this.myData = await axios.get("http://127.0.0.1:8000/api/profile", {
  27.         headers: { Authorization: `Bearer ${this.myToken}` },
  28.       });
  29.     } catch (error) {
  30.       if (error.response) {
  31.         // console.log(error.response.data);
  32.         // console.log(error.response.status);
  33.         // console.log(error.response.headers);
  34.         if (error.response.status == "403")
  35.           //unauthorized
  36.           this.$router.push({ name: "Home" });
  37.       }
  38.     }
  39.  
  40.     this.myData = this.myData.data.data;
  41.  
  42.     for (const key in this.myData) {
  43.       // console.log(`${key}: ${this.myData[key]}`);
  44.       if (
  45.         !(
  46.           this.myData[key] == "" ||
  47.           this.myData[key] == "null" ||
  48.           this.myData[key] == undefined ||
  49.           this.myData[key] == null ||
  50.           key == "pets"
  51.         )
  52.       )
  53.         this.filteredData[key] = this.myData[key];
  54.     }
  55.  
  56.     console.log(this.myData.pets);
  57.  
  58.     if (this.myData.pets) {
  59.       this.myData.pets.forEach((element) => {
  60.         if (this.filteredData.pets)
  61.           this.filteredData.pets =
  62.             this.filteredData.pets + `${element.name} ${element.type}, `;
  63.         else this.filteredData.pets = `${element.name} ${element.type}, `;
  64.       });
  65.     }
  66.  
  67.     this.myData = [this.filteredData];
  68.  
  69.     this.showData = true;
  70.   },
  71. };
  72. </script>
  73.  
RAW Paste Data