Tips for sending arguments to an async function

I am currently facing an issue in my vue.js application where I am struggling to access the value of name that is being passed to my function. My hunch is that this issue is related to scope. After some research, it seems like using a fat arrow function might help resolve this problem?

Below is the code snippet I am using to handle a change event. Is there a way to

async handleChange(event, name) {
    console.log('name: ', name);  // works
    console.log('value: ', event.value);  // works

    try {
        let response = await axios.patch(`/my/path`, {
            name: event.value,  // need to extract the value from 'name'
         });

         if (response.status === 200) {
             //
         } else {
             console.error('Error: could not update. ', response);
         }
     } catch (error) {
         console.error('Error: sending patch request. ', error);
     }
}

I have also attempted to refactor it like so:

handleChange: async (event, name) => {
    ...
}

I am uncertain about how to integrate a fat arrow function within the axios patch. Any guidance would be greatly appreciated. Thank you!

Answer №1

The instructions are a bit unclear, but it seems like you are trying to use the name as the key within an object. If that's the case, you can achieve it by following these steps:

const result = await fetch(`/my/endpoint`, {
   [name]: inputValue,
});

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

CORS policy is preventing preflight requests to Cloud functions using the OPTIONS method

Currently, I am working on a project to develop a basic app where users can upload a .pdf file on the front end. The server will receive the file and process it until it generates a Firebase storage link. The front end is hosted on Firebase Hosting, while ...

Preserving scroll position when updating a partial page template using Rails and AJAX

When I am utilizing long polling, I encounter an issue where every time I use AJAX to refresh a page partial inside a scrollable div, the contents automatically scroll to the top. Is there any way to load the partial while maintaining the current scroll ...

Aggregate the data entered into input fields on one page and display them on a separate page

Can you help me with this task? - I have 2 pages set up (page 1 has input fields & page 2 is where the entered data should be displayed) - I want to retrieve all the text input from the first field and insert it into the "content" tag on the second page. ...

Is it possible for me to search within a retrieved document using Mongoose?

My schema is structured as follows... var TerritorySchema = new Schema({ user: Schema.Types.ObjectId, streets: [streets_schema] )}; var StreetsSchema = new Schema({ name: String, odd: [block_schema], even: [block_schema], tags: [S ...

sending a string of JSON in PHP (including quotes) to an onclick event function

I am faced with the challenge of passing an array of data from PHP to JavaScript for the "onclick" event. The approach I took was to convert the array data into a JSON string which could then be parsed back in the JavaScript function for manipulation. How ...

What could be causing the issue with Google Chart in my ASP MVC app?

My controller has a method that returns Json data. [HttpPost] public JsonResult CompanyChart() { var data = db.adusers; var selectUsers = from s in data where (s.Company != null) select s; int f ...

Struggling to navigate the world of JavaScript and find the sum of odd numbers?

Currently facing a roadblock with a codewars exercise and in need of some assistance. The exercise involves finding the row sums of a triangle consisting of consecutive odd numbers: 1 3 5 7 9 11 13 15 17 ...

Disable automatic playback of HTML video

There is an HTML video with an image that loads initially and then disappears to play the video. I would like the image to always be visible until I click on it. Once clicked, the video should start playing. You can view the code on JSFiddle: http://jsf ...

Tips for concealing overlay when the cursor hovers

Can anyone help me with a code issue I'm having? I want to hide an overlay after mouse hover, but currently it remains active until I remove the mouse from the image. Here is the code: .upper {position: absolute; top: 50%; bottom: 0; left: 50%; tra ...

From transitioning from AngularJS to the latest version Angular 8

I have an Angular application where I need to update some old AngularJS code to work with Angular table.html <table ngFor="let group of vm.groups" style="float: left"> <thead> <tr> <th><b>Sl. No</b ...

What steps should be taken to complete orders following the checkout.session.completed event triggered by Stripe?

Having an issue with Stripe's metadata object that has a limit of 500 characters. My checkout flow is operational, but the only constraint is the character limit for my cart. I need to include extras and customer notes in my cartItems object for each ...

Combining Repetitive Elements in an Array

Trying to combine an array of products with the same order_id while also including all objects from a second products array. Below are some sample orders: const orders = [ { "order_details": { }, "order_id": "1", ...

What steps do I need to take in order to transform this code into a MUI component complete with

Having some trouble converting my hero banner code to MUI in a React project. The styling is not coming out correctly. Here is the HTML code: <svg data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1200 120 ...

Is there a way to alphabetically and numerically organize a table in vue.js?

Currently, I am implementing sorting functionality for a table using vue.js. While I have successfully achieved ascending sorting for numbers, I am facing challenges with getting the descending and alphabetical sorting to function properly. Below is the H ...

Modify the button's border color upon click action

I am looking to implement a feature where the border of a button changes when clicked once and reverts back when clicked again. This should apply individually to each of the 16 buttons with the same class. Additionally, I want to enable the ability to clic ...

What is the most effective method for incorporating web APIs (such as setTimeout, fetch, etc.) within the V8 engine?

Currently, I am tackling a project that requires the use of v8 in Go for running JS code. To achieve this, I am utilizing the v8Go library. The challenge I am facing is the inability to utilize functionalities like fetch, setTimeout, and other Web APIs. Wh ...

When errors occur while printing HTML through an Ajax request, it can hinder the functionality of other JavaScript code

I recently conducted an interesting experiment on my website. The concept involved sending an AJAX request to a PHP file, which then retrieved a random website by using various random words for search queries on Google. The retrieved website content was th ...

Using a combination of stringify, regular expressions, and parsing to manipulate objects

As I review code for a significant pull request from a new developer, I notice their unconventional approach to editing javascript objects. They utilize JSON.stringify(), followed by string.replace() on the resulting string to make updates to both keys a ...

Adjusting HTML5 drag height while resizing the window

Code conundrum: var dragHeight = window.innerHeight - parseInt(jQuery("#drag_area").css("margin-top")) - 5;. It sets the drag height based on browser size, but there's a glitch. If I start with a non-maximized browser and then maximize it, the drag he ...

Adjust the text size for groupBy and option labels in Material UI Autocomplete without altering the size of the input field

Currently, I am utilizing Material-UI and ReactJS to implement grouped autocomplete functionality. See the code snippet below: import * as React from "react"; import TextField from "@mui/material/TextField"; import Autocomplete from &q ...