Vue.js axios API call throwing error due to undefined property 'map'

Attempting to call a data source from a remote URL results in the error

TypeError: Cannot read property 'map' of undefined
. The axios method is enclosed within axios.js:

.....
result = new Promise((resolve, reject) => {
      axios.get(url)
        .then((response) => {
          const processedData = response.data.response.map((v) => {
            return v;
          })
          resolve(processedData);
        }).catch((error) => {
          if(typeof(error) == 'object'){
            alert(error) // @@@ I ALWAYS LAND HERE
          }
          reject(error.data); 
        });
    });
 ....

Verification in the network console confirms that the API URL being accessed is functioning properly and all data can be viewed. The format of the API is as follows:

[
    {
        "id": 1,
        "name": "alpha",
        "build_id": 50,
        "app_env_names": "",
        "app_env_list": [
            ""
        ],
    },
    {
        "id": 2,
        "name": "feature/OPS-05",
        "build_id": 48,
        "image_tag": "feature_DEVOPS-605-jasc-cr-approval-test-71496674-48",
        "app_env_names": "dev",
        "app_env_list": [
            "dev"
        ],
       .....

Attempts to remove map have been unsuccessful. Seeking advice on resolving this issue. Thank you!

Answer №1

Correction: Avoid referring to the code below the marked line. It appears to be using a promise construction pattern that may not handle all edge cases effectively.

Instead, consider using

response = axios.get(url).then(({ data }) => data)
.

================================================================

After some quick investigation (thanks to @Phil), I realized my mistake as soon as I published the question. By removing the unnecessary response, the solution simplifies to:

result = new Promise((resolve, reject) => {
        axios.get(url)
          .then((response) => {
            const processedData = response.data.map((v) => {
              return v;
            })
            resolve(processedData);
          }).catch((error) => {
            if(typeof(error) == 'object'){
              alert(error)
            }
            reject(error.data);
          });
      });

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

Error with JavaScript callback functions

After creating a POST route, I encountered unexpected behavior in the code below. The message variable does not display the expected output. app.post("/", function (req, res, error) { var message = ""; tableSvc.createTable("tableName", function (error ...

What could be causing my CORS fetch request to not send cookies to the server?

Trying to work out a CORS request using the fetch method: fetch('https://foobar.com/auth', { method: 'GET', mode: 'cors', credentials: 'include', }) The server-side code in express for impl ...

Using JavaScript to utilize a variable containing a .match method with Regex Expression

Recently, I started delving into the world of regex with the aim of incorporating variables in my matches. Imagine I have a string that reads "Total: $168" My goal is to extract just the numerical value; 168. This is what I currently have: totalCost = t ...

Dynamic, AJAX-driven content is experiencing a 200 status code

Is it necessary for a single-page website, which dynamically loads content via ajax and utilizes browser session history stacking, to return a "200 Status" for each successful state change? The core code of my website is as follows: $(document).on('c ...

loading content into a pop-up window with JavaScript

Within my application, I am utilizing jQuery Ajax to retrieve a list that will be displayed in a popup. The javascript code returns a list structured as [["1","abc"],["2","bcd"]]. This list is then being used to populate the contents of the display tag wit ...

What is the rationale behind not storing OAuth2 access tokens as HttpOnly secure cookies? How could this approach be implemented in a Node.js application?

I have a Node.js RESTful api setup where the response token received after posting to /oauth/token typically looks like this: { "refresh_token": "eyJraWQiOiI2...", "token_type": "Bearer", "access_token": "eyJraWQiOiI2Nl...", "expires_in": 3600 } ...

Implement jqGrid on dynamically generated HTML content

I am currently facing an issue with a modal dialog that loads its content using an AJAX call. Within the loaded content, there is HTML markup that utilizes jqGrid. $.get('@Url.Action("ListPartial")',null, function(data, txtStatus, jqXHR) { ...

Using D3 to access a nested select and sibling from the JSON property of an object

I am attempting to include a sibling before a series of div elements that have been generated using the .enter() method. Here is my current setup: const data = [ { "name": "foo", "hierarchies": [ { "name": " ...

Tips for updating a plugin from phonegap 2.5 to the most recent version 3.1, and the steps to follow when including a new plugin in phonegap

$('#sendSms').click(function(e){ alert("Sending SMS in progress");//After this alert, I encountered continuous errors and couldn't determine the root cause var smsInboxPlugin = cordova.require('cordova/plugin/smsinboxplugin'); ...

Transforming the date from JavaScript to the Swift JSON timeIntervalSinceReferenceDate structure

If I have a JavaScript date, what is the best way to convert it to match the format used in Swift JSON encoding? For example, how can I obtain a value of 620102769.132999 for a date like 2020-08-26 02:46:09? ...

After closing and reopening the jQuery modal, the name does not appear

I crafted a custom modal window to display images along with comments and the owner's name. The issue arises when I close the current modal and reopen it; the owner's name disappears. Oddly enough, the name is displayed the first time the modal i ...

What is the reason for webpack adding "-es5" and "es2015" to my TypeScript files?

When Webpack converts my typescript files into Javascript files, it appends -es5 to the name. For instance, I expect the file to be named background.js, but it actually generates 4 separate files: background-es5.js background-es5.js.map background-es2015. ...

Implementing Bootstrap JavaScript functionality within a Rails 7 application

My current Rails 7 application utilizes esbuild as the JS bundler and has bootstrap imported. I am facing an issue where I am unable to access any Bootstrap Javascript functionality outside of the main application.js file. For example, I am attempting to p ...

Struggling to store information in a MongoDB database within my MEAN Stack project

After successfully creating a collection for user LOGIN/LOGOUT and managing to store and retrieve data using Express app and mongoose, I encountered an issue when trying to create another collection. Despite successfully creating the collection, the data w ...

How can we use vanilla JavaScript to implement an image zoom effect on mouse movement?

I'm attempting to implement an onmousemove event within an image that displays a zoomed image on the right side. Issue: When I move my mouse inside the image, the black box and zoomed image flicker. The zoomed image does not align correctly in the b ...

Is there a way to remove a link to an image that pulls data from another website?

On my HTML page, I have implemented the following code to display weather data: <!-- Begin Weather Data Code --> <div style="display:none;"> <a href="http://iushop.ir"> <h1>Weather</h1> </a> </div> < ...

The body-parser module in express 4.0 is currently not accessible for the route handler

I am attempting to send a PUT request via Google Postman in the following format: PUT /updateUser/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="debcbcbc9eadadadf0bdb1b3">[email protected]</a> HTTP/1.1 Host: loca ...

Transmit a file using multipart with XMLHttpRequest

Is it possible to use XMLHttpRequest to send a multipart file to a servlet? I am currently working on a form that needs to be submitted as multipart, but I am not receiving a response after successfully uploading it. It is important that the process happe ...

Unable to generate a three-dimensional plane on the XZ plane

When using the Aframe library to create a plane, I defined the following points: point_1 = [0, 0, 0]; point_2 = [1, 0, 0]; point_3 = [1, 1, 0]; point_4 = [0, 1, 0]; To generate a plane in the XY plane, everything functions as expected. Here is the workin ...

Angular JS merges various records under a common category from a JSON document

[{"Category":"cat","Value":"large cat"}, {"Category":"cat","Value":"small cat"}, {"Category":"dog","Value":"large dog"}, {"Category":"dog","Value":"little dog"}, {"Category":"dog","Value":"cute dog"}] If I have a JSON file structured like this, how can I ...