Retrieve a string using Axios and update an object with the same input in Vue 3

I am currently faced with a situation where I have a table that receives the userList as props and renders it using v-for="(user, index) in userList" :key="index" to display the username. However, my Swagger API now requires me to update the data structure to something like username:{value: "", operand: 1} when making a patch request. Can anyone provide guidance on how to change the data type and send it as formData to my axios patch request? My axios.patch method looked different before this requirement was discovered :')

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

The input form used is structured as follows:

<input type="text" class="text-xs text-secondary mb-0 text-center
                  border-0 bg-white w-100 p-1"
        :disabled="!user.disabled"
        :value="user.nationalCode">

Answer №1

To streamline the data handling process, consider declaring an object variable within the data section where you can assign various items to it. For instance, define an object called items in the data and then proceed to specify individual properties such as items.username and items.email. When preparing to transmit this object to the server, you can easily convert these properties by implementing a for loop. Here's an 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)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Stopping errors are a common occurrence in synchronous AJAX

I recently encountered an issue with my AJAX request setup. In the success callback function, I called a new function to render Google links and made another AJAX call. To address some performance concerns, I attempted to change these asynchronous calls t ...

Using Javascript in ASP.NET MVC to set an uploaded image as the background

I have successfully uploaded images in my application to the server. Now, I am looking to dynamically set an uploaded image as a background using JavaScript in ASP.NET MVC5. Below is my code snippet where it reads file from local storage: jQuery(document) ...

Fetch data using useSWR with Axios will return the entire response object instead of just the

Currently exploring the benefits of useSWR, I am facing challenges with refactoring that are giving me a headache. One specific issue is not receiving the expected data from the hook. In one instance, the proper data is fetched, while in another, the entir ...

Issue with implementing setTimeout on await fetch() result

I'm trying to figure out how to add a setTimeout to my request response when using await fetch. Currently, each response is being sent to a DIV in the HTML, but I want to introduce a delay of 5 seconds before each response appears. I attempted this s ...

Is there a way to utilize code to invoke a method of an object? For example, calling MyObject.XVariable(//some function

I have a unique object that gets its data filled from an API call. let MyDog = { Name: 'Dog', } let arrayFunctions; fetchDogsFunctions(dogAPIUrl).then(res => { // The results variable contains an array of functions the dog can do, such as get ...

What is the way to utilize JavaScript for parsing Ruby JSON strings?

I am currently attempting to extract the JSON object from a string that has been outputted in JSON format from a Rails application. In my JavaScript code, I have the following: data = "<%= @chromosomes.html_safe %>"; The issue I am facing is that d ...

With Vue 3 Pinia, values fetched from an API within a Pinia store cannot be statically assigned; they will always be reactive

I have a frontend built with vue3 and using pinia as the store. I am setting it up as shown below: export const useShopStore = defineStore( "shop", () => { const state = reactive({ data: { fruits: [], owners: [] ...

Having difficulties accessing the /playlists route with express and app.get in my code

I am encountering a 404 status code error when trying to access app.get('/playlists'). The browser displays "cannot GET /playlists" and I can't seem to figure out why. Here is the code I am using: var express = require('express&apos ...

Storing API data in localStorage using VueJS: A step-by-step guide

As I work on practicing building a simple SPA with VueJS, one of my current tasks involves listening to an API and storing certain data in the browser's localStorage. However, my experience with VueJS is still limited, so I'm unsure about how to ...

Making adjustments to a Jquery data attribute can have a direct impact on CSS

My CSS is using a data attribute, but when I try to change it with jQuery, the change is not reflected on the screen or in the DOM. $('#new-1').data('count', 5); .fa-stack[data-count]:after { position: absolute; right: -20%; to ...

Mobile site experiencing owl.carousel responsiveness issues after refreshing the page

I am currently working on a website located at . On the homepage, right after the slider, there is a carousel of three info boxes. However, in mobile view (developer mode), after a hard refresh or viewing the link on an actual mobile device, it displays a ...

Generate an animated sequence transitioning smoothly from the left side to the center

I am in need of creating an animation that will cause a menu to appear from the left side of the screen when a button is clicked. The menu should expand to cover 55% of the width of the main page. You can view a demo of this animation on JSFiddle by follo ...

The `toString` function is displaying an array instead of the expected string

As a beginner in programming, I am currently enrolled in an introductory JavaScript course. However, I am facing an issue with my assignment where the toString method is printing an array instead of a string. Despite thorough research, I have been unsucces ...

Large data sets may cause the Highchart Bar to not display properly

Currently, I am working on a web project that displays traffic usage in chart mode using Highchart Bar. The issue I am facing is that there are no errors thrown when running this code. <script type="text/javascript"> $(function () { $(&apos ...

Having issues with TableExport.js exporting Bootstrap HTML tables

I've been trying to use the TableExport.js plugin found at to add Bootstrap HTML table export functionality to my website. I meticulously followed all the steps to include jquery FileSaver, tableExport javascripts, and css. <!-- jQuery --> &l ...

Creating independent Javascript applications and HTML5 canvas games, optimized for V8 or tailored to the specific browser for optimal performance

Instead of solely targeting Chrome users due to browser performance issues, I am exploring alternative options to create an executable version of a canvas game. Is there a way to integrate v8 for non-Chrome users? Perhaps a web "wrapper" similar to Flash&a ...

Limiting joined rows in Sequelize ORM for a many-to-many association: A helpful guide

Sequelize defines two models: Post and Tag, connected by a many-to-many association. Post.belongsToMany(db.Tag, {through: 'post_tag', foreignKey: 'post_id', timestamps: false}); Tag.belongsToMany(db.Post, {through: 'post_tag' ...

I am looking to connect the contents of a subfolder page with a page located in the main folder

I need to redirect the login page located in the subfolder signup to the index page within the main folder called web. The web folder contains both the index page and the signup folder, while the login page resides in the signup folder. What steps should ...

Managing the close event of a lightbox

A lightbox is present on the page with a close icon that has a class of .closeIt. When the user clicks the close icon, the focus should shift back to the link element that triggered the lightbox. An attempt was made to create a function that takes the tri ...

Transfer information from .gs (Google Apps Script) to a variable inside a <script> tag in an HTML document

[ {"id":1,"label":"Node 2"}, {"id":2,"label":"Node 3"}, {"id":3,"label":"Node 4"}, {"id":4,"label":"Node 5"} ] Hello there! The function getArray() in the code snippet above is returning the specified string ↑. I need help connecting this data w ...