Home > Mobile >  how can i update an object id in vuejs
how can i update an object id in vuejs

Time:09-06

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

<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

CodePudding user response:

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.

Change it to:

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