Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can i update an object id in vuejs

i’m trying to set an address id as default, i’m getting a success message but each time i check the user json object the address id doesn’t seem to update

this is my script tag

<script>
import axios from "axios";
export default {
  data() {
    return {
      addresses: [],
       message: "",
    };
  },
  methods: {
    onSetDefault(id) {
      const token = localStorage.getItem("token");
      axios
        .put(`http://localhost:5000/api/addresses/set/default`, token, {
         id: id,
          headers: {
            Authorization: "Bearer" + token,
            "x-access-token": token
          }
        })
        .then(res => {
   this.message = res.data.message;
          console.log(id);
          console.log(res);
        })
        .catch(error => {
          this.message = error.message;
          console.log(error);
        });
    }
  }
};
</script>

this is my template

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<div
      v
      v-for="(address,index) in addresses"
      :key="address._id"
    >
      <!-- Set Address as Default -->
      <a
        href="#"
        @click="onSetDefault(address._id)"
      >Set as Default</a>
    </div>

this is the response i get

Successfully set this address as default

but the address id doesn’t update to the one i set as default

>Solution :

I guess there is an error in your axios call. You are sending token as the payload / body of the request instead of the id. And you send the id as a part of the options object. See axios docs.

axios
   .put(`http://localhost:5000/api/addresses/set/default`, { id },{ headers: {
            Authorization: "Bearer" + token,
            "x-access-token": token
          }
        })
   .then (…)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading