Home > Back-end >  axios get string and patch as object from the same input vue 3
axios get string and patch as object from the same input vue 3

Time:06-25

I have a table that I get the userList as props and render it into the table with v-for="(user , index) in userList" :key="index" and as string :value="user.username" but now my swagger API needs me to patch it as an object of s.th like username:{value: "", operand: 1} does anyone know how am I supposed to change the data type and send it as formData to my axios patch? my axios.patch was also like this before i find out I need an object to pass :')

async userEdit(id) {
      const response = await this.$store.dispatch('axiosPatch', {url: `/employees/${id}`,
        formData: {
          username: this.username.value,
          email: this.email,
          mobile: this.mobile,
        }
    }, this.$data)
   },

my input form is also like this:

<input type="text" 
        :disabled="!user.disabled"
        :value="user.nationalCode">

CodePudding user response:

You could declare an object variable in the data part and then assign items to it for example we have items object defined in data then we have items.username,items.email, and so on, at the end when you want to send this object to the server, you could convert all of these by doing a for loop on it. for example:

let dataToPatch = [];
let items = {username:'Ali',email:'[email protected]'};
for(item in items){
  const newItem = {};
  newItem[item] ={value:items[item],operand: 1};
  dataToPatch.push(newItem);
}

console.log(dataToPatch)

  • Related