Receiving an undefined response from Axios get request

I've encountered a peculiar issue while working with axios get calls.

try {
        console.log('assetAddress', assetAddress);
        var options = {
            method: 'GET',
            url: `https://testnets-api.opensea.io/api/v1/events?only_opensea=false&limit=20&asset_contract_address=${assetAddress}`,
            headers: { "Accept": 'application/json' }
        };
        console.log(options.url)
        res = await axios.request(options);
        return res.data;
    } catch (e) {
        //console.log(e)
    }

When I include assetAddress in the url and attempt to retrieve the result, it returns undefined. However, if I manually input the actual address 0x15352F80426ec9b94412b45242d7040b5dFeB5E6, it works fine.

Any idea why this happens?

Answer №1

It appears that the issue lies with the scope of your assetAddress variable.

Here are two examples showcasing two different approaches


Instead of using async / await, you can utilize the .then and .catch functions in the following manner:

const assetAddress = "0x15352F80426ec9b94412b45242d7040b5dFeB5E6"

var options = {
  method: 'GET',
  url: `https://testnets-api.opensea.io/api/v1/events?only_opensea=false&limit=20&asset_contract_address=${assetAddress}`,
  headers: {
    "Accept": 'application/json'
  }
};
axios.request(options)
  .then(res => console.log(res.data))
  .catch(error => console.log(error))
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/1.0.0-alpha.1/axios.min.js"></script>


If you prefer the classical approach, you can still achieve the same functionality by:

const assetAddress = "0x15352F80426ec9b94412b45242d7040b5dFeB5E6"

async function loadData() {
  console.log("Loading Data")
  var options = {
    method: 'GET',
    url: `https://testnets-api.opensea.io/api/v1/events?only_opensea=false&limit=20&asset_contract_address=${assetAddress}`,
    headers: {
      "Accept": 'application/json'
    }
  };
  try {
    const res = await axios.request(options)
    console.log(res)
  } catch (error) {
    console.log(error)
  }
}

document.getElementById('loadDataButton').addEventListener('click', loadData)
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/1.0.0-alpha.1/axios.min.js"></script>

<button id="loadDataButton">Load data</button>

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

HTML - Selecting Different Values in One Drop Down Based on Another Drop Down

When selecting "First Year" in the initial drop-down menu, the options "Sem1" and "Sem2" should be displayed in the second drop-down menu. Similarly, when choosing "Second Year" in the first drop-down menu, the choices "Sem3" and "Sem4" should appear in th ...

Determining if a URL links to an image when the file extension is not informative

I am currently working on building an AJAX call to retrieve data from an API with a messy data structure. The challenge I'm facing is that the array returned by each AJAX call can contain up to 30 elements, some of which have image URLs without a file ...

Angular 4 showcases the information stored within this dataset

The data returned from an API to my Angular 4 application is not to my liking. Here is an example of the JSON, where I am only interested in the coin and its price: Goal is to display this data on the page: Coin Price BTC $4,281.28 ETH $294.62 ...

Modifying the color of elements within a picture

I am in search of the perfect web technology or JavaScript library that can help me achieve a specific functionality. My aim is to change the colors of particular objects within an image. I am looking to create a tool where users can select a color, and th ...

What could be causing the issue of this JQuery dropdown plugin not functioning properly on multiple dropdowns?

I have implemented a JQuery plugin for dropdown menus that can be found at the following link: https://code.google.com/p/select-box/ Currently, I am facing an issue where the script only seems to work for the first dropdown menu out of 4. I am unsure abo ...

Encountering difficulties with the functionality of Google Places API

I am attempting to incorporate the autocomplete functionality of Google Places API into a text field. Below is the code I have implemented: <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script> <script ...

Tips for filtering data using an array within an object containing arrays

Below is the provided information: userRoom = ['rm1']; data = [{ name: 'building 1', building: [{ room: 'rm1', name: 'Room 1' },{ room: 'rm2', name: ' ...

Is there a way to avoid adding this final "faulty state" to the array?

While working on dynamically slicing an array, I noticed that my while loop was causing an extra slice to be added when it broke the conditional. Even though I can achieve the desired functionality by removing the last element with arr.pop(), I am curious ...

jQuery for validating input fields with positive integers only using regular expressions

I currently have two input fields in my HTML that look like this: <input type="text" class="txtminFeedback" pattern="^\d+([\.\,][0]{2})?$" placeholder="Minimum Feedback"> <input type="text" class="txtmaxFeedback" pattern="^\d ...

Including a JavaScript file using script tags can cause issues with jQuery UI

I've been working on this issue for quite some time now and it's proving to be quite challenging. The problem I'm facing involves incorporating jQuery UI's datepicker into my HTML page which is already being controlled by some Javascri ...

Combining React state value with an HTML tag would be a great way

I am facing an issue while trying to concatenate the previous value with new data fetched from an API using a loop. Instead of getting the desired output, I see something like this: [object Object][object Object],[object Object][object Object] Here is ...

Display popup just one time (magnific popup)

Attempting to display this popup only once during a user's visit. It seems like I might be overlooking something. <script src="http://code.jquery.com/jquery-1.7.min.js"> <link href="http://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1 ...

Injecting AngularJS service into a karma-mocha test: Step-by-step guide

I have developed a Karma-Mocha test that involves injecting an AngularJS service. Service Code: (function() { 'use strict'; angular.module('portal').service( 'AmountFormatService', functio ...

Having difficulty obtaining accurate window height in Chrome

I am currently facing an issue with determining the accurate width and height of the browser window in Google Chrome. Interestingly, the size appears to be correct in Firefox, but I have not tested it on other browsers. Despite setting the doctype to !DOC ...

Why does Javascript in Angular throw an exception when using value, match, and replace functions together?

I have a small JavaScript function that I would like to implement in an Angular service or controller. function cprCheck(frm) { var cpr = frm.cpr.value; if (cpr.match(/[0-9]{6}\-[0-9]{4}/)) { cpr = cpr.replace(/\-/g, ""); var chk = 0; ...

Unable to display an image element in fullscreen within a modal that is already fullscreen

I am facing an issue with my modal that is designed to occupy the full width and height of a browser. Inside this modal, there is an image enclosed within a div along with some other elements. My goal is to make the image go full screen in the browser, aki ...

Tips for adding a CSS class to an HTML element on-the-fly

As a newcomer to JavaScript and AngularJS, my question may be considered naive by some. I am currently working on a tutorial project involving AngularJS. Here is the HTML code snippet I am dealing with: <link href="http://netdna.bootstrapcdn.com/boo ...

The rule "react/jsx-sort-props" does not have a valid configuration

I've been attempting to organize props names alphabetically using the eslint-plugin-react plugin but I keep encountering this error: [Error ] .eslintrc.json: Configuration for rule "react/jsx-sort-props" is invalid: Value {"callbacksLast":true,"shorth ...

The useEffect() method used without any cleanup function

It is mentioned that, "Every time our component renders, the effect is triggered, resulting in another event listener being added. With repeated clicks and re-renders, numerous event listeners are attached to the DOM! It is crucial to clean up after oursel ...

Angular: Defining variables using let and var

When working with TypeScript and JavaScript, we typically use either let or var to declare a variable. However, in Angular components, we do not use them even though Angular itself uses TypeScript. For instance, export class ProductComponent implements OnI ...