0

Using fetch() and the Ticketmaster API I'm trying to access [[PromiseValue]] in the log, but keep getting undefined.

const API_key = '1234'

fetch(`https://app.ticketmaster.com/discovery/v2/events.json?size=1&apikey=${API_key}`)
      .then(response => console.log(response.json()))
      .then(data => console.log(data))
3
  • 1
    Is this code failing in some way? What exactly isn't working? Commented Nov 4, 2019 at 19:38
  • Sorry, I had to rephrase the question because I was not even at that step yet. What I'm trying to do currently with this API is access [[PromiseValue]] in the log, but I'm getting undefined. Commented Nov 4, 2019 at 19:52
  • It's still not clear what you're describing. What is "[[PromiseValue]]"? What is being returned by the server in this request? Commented Nov 4, 2019 at 19:55

1 Answer 1

1

.then(response => console.log(response.json())) is returning undefined as the console.log method doesn't return any value;

Update your code to use the actual value:

Option 1:

fetch(https://app.ticketmaster.com/discovery/v2/events.json?size=1&apikey=${API_key})
  .then(response => {
      var response = response.json();
      console.log(response);
      return response;
  })
  .then(data => console.log(data))

Option 2:

fetch(https://app.ticketmaster.com/discovery/v2/events.json?size=1&apikey=${API_key})
  .then(response => response.json())
  .then(data => console.log(data))

If you further wanted to utilize the data returned from the API, you have to understand what the shape of the data is and define what you want to do with it. Looking at their docs quickly, if you want to get the name of the first event returned in your query, you could replace .then(data => console.log(data)) with

.then(data => console.log(data._embedded.events[0].name)) // logs it to the console

OR

.then(data => alert(data._embedded.events[0].name)) // creates an alert with the name in it

OR

.then(data => {
  var ele = document.createElement('div');
  ele.innerHTML = data._embedded.events[0].name;
  document.querySelector('body').appendChild(ele);
}); // creates a div with the name in it and appends it to your page
Sign up to request clarification or add additional context in comments.

4 Comments

thanks, now from this how would I go about accessing data from the log? sorry I'm a new programmer and using API's are very new challenging for me.
Happy to help, but it is very unclear to me what you are trying to do. You wouldn't access data from the log. Logging is one useful tool that can help figure out what is happening if you are having problems. In the line data => console.log(data), data is your data, so you would do whatever you want with it there. You can log it to the console(as is being done above) or something else, but you will need to provide more details in order to get more help.
Ok, I'm trying to access data in the Ticketmaster API and I'm not sure what the next steps would be. I know this is a pretty general question, but more specifically I'm trying enable a search for events in the Ticketmaster API.
Updated my answer with some options, but you still aren't being that specific. Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.