Vue edit name of prop without losing functionality

Viewed 30

So I am working to do a post with Vue and my REST API (spring boot). If I do this post from Postman I get what I want: My postman Post

Now I wanted to this via my VUE project like this:

My Vue page:

<template>
  <div class="submit-form">
    <div class="form-group">
      <label for="coalition">Coalition Name</label>
      <input
          class="form-control"
          id="coalition"
          required
          v-model="coalition.name"
          name="coalition"
      />
    </div>


    <div class="form-group">
      <label for="date">Date</label>
      <input
          type="date"
          id="date"
          required
          v-model="coalition.date"
          name="description"
      />
    </div>

    <button @click="saveCoalition" class="btn btn-success">Submit</button>
    <p>{{message}}</p>

    <!--    <div>-->
    <!--      <button class="btn btn-success" @click="newParty">Add</button>-->
    <!--    </div>-->
  </div>
</template>

<script>
import CoalitionDataService from "../../services/CoalitionDataService";
import jwt_decode from "jwt-decode";
import HomeDataService from "../../services/HomeDataService";
export default {
  name: "AddCoalition",
  data() {
    return {
      coalition: {
        id: null,
        name: "",
        date: "",
        account:{
          id: ""
    },
      },
      message:""
    };
  },
  methods: {
    getUserId(){
      //Retrieve JSON WEB TOKEN
      try {
        var token = localStorage.getItem("Authorization")
        var decoded = jwt_decode(token);
        var username = decoded[Object.keys(decoded)[0]];
        HomeDataService.getId(username)
            .then(response => {
              this.account.id = response.data.id;
              console.log(this.account.id)
            })
      }
      catch (e){
        console.log("No user is logged in")
      }
      return this.account.id
    },
  saveCoalition() {
    var data = {
      name: this.coalition.name,
      description: this.coalition.description,
      date: this.coalition.date,
      account:{
        id: this.getUserId()
      },
    };

    CoalitionDataService.create(data)
        .then(response => {
          this.coalition.id = response.data.id;
          console.log(response.data);
          this.message = 'The Coalition was created successfully!';
        })
        .catch(e => {
          console.log(e);
        });
  },

  newParty() {
    this.coalition = {};
  }
}

}
</script>

But now I think the problem is my program does not distinguish account.id from id. How should I change it so that I can do the post like in the postman, but my program does not error on this.

The error

Error

Thanks in advance!

Note

I saw that if I clicked my id from account like this: The other id gets highlighted as well like they are the same. Maybe this helps.

Image

2 Answers

You are returning this.account.id inside your getUserId function before it actually has a value. return this.account.id happens before it gets a value (this is asynchronous)

You can chain the promises to get the values in correct order, so something like this:

getUserId(){
  try {
    var token = localStorage.getItem("Authorization")
    var decoded = jwt_decode(token);
    var username = decoded[Object.keys(decoded)[0]];
    return HomeDataService.getId(username).then(res => res.data.id)
  }
  catch (e){
    console.log("No user is logged in")
    return e;
  }
},
saveCoalition() {
  this.getUserId().then(id => {
    var data = {
      name: this.coalition.name,
      description: this.coalition.description,
      date: this.coalition.date,
      account:{
        id: id
      },
    };
    return CoalitionDataService.create(data)
  })
  .then(.....)
}

it seems that account is located inside the coalition object in your data object so this.account returns undefined therefore the error "cannot read property 'id' of undefined".

try addressing like this: this.coalition.account.id and see if it works or not

Related