4

I have an issue to request the Cycle Number from this API. I don't understand how to use the map to get the data from the last cycle only.

 const ul2 = document.getElementById('Cycle');
  const url2 = 'https://api6.tzscan.io/v3/rolls_history/tz1NortRftucvAkD1J58L32EhSVrQEWJCEnB?number=1&page=0';
  fetch(url2)
  .then((resp) => resp.json())
  .then(function(data){
      data.map(
      let span = document.createElement('span');
      span.innerHTML = `${data.cycle}`;
      ul2.appendChild(span)   
  })
  .catch(function(error) {
    console.log(error);
  });

https://codepen.io/anon/pen/aMXeYJ

3

The first element returned from https://api6.tzscan.io/v3/rolls_history/tz1NortRftucvAkD1J58L32EhSVrQEWJCEnB?number=1 is the data from the latest cycle.

HTML

<h1>Last Cycle</h1>
<div id="cycle"></div>
<div id="rollCount"></div>
<div id="rollTotal"></div>

JavaScript

const url = 'https://api6.tzscan.io/v3/rolls_history/tz1NortRftucvAkD1J58L32EhSVrQEWJCEnB?number=1&page=0';
const cycleEl = document.getElementById('cycle')
const rollCountEl = document.getElementById('rollCount')
const rollTotalEl = document.getElementById('rollTotal')

fetch(url)
.then(resp => resp.json())
.then(([lastCycle]) => {
  cycleEl.innerText = lastCycle.cycle
  rollCountEl.innerText = lastCycle.roll_count
  rollTotalEl.innerText = lastCycle.roll_total
})
.catch(console.log);

https://codepen.io/RichardAyotte/pen/MxRWJG

0

This is more of a Javascript question then a Tezos question, but anyway;

 const ul2 = document.getElementById('Cycle');
  const url2 = 'https://api6.tzscan.io/v3/rolls_history/tz1NortRftucvAkD1J58L32EhSVrQEWJCEnB?number=1&page=0';
  fetch(url2)
  .then((resp) => resp.json())
  .then(function(data){
    if (data.length > 0) {
      let span = document.createElement('span');
      span.innerHTML = `${data[0].cycle}`;
      ul2.appendChild(span)
    }
  })
  .catch(function(error) {
    console.log(error);
  });

If you ever get multiple items in the returned data array you need to sort the array before picking the correct element.

  • Thanks for you help but it doesn't work ! I try all the way possible ! Do you think I should better ask that on the JavaScript stackexchange ? – Lexxor Mar 25 at 16:05
  • Perhaps I misunderstand what you are trying to do. Did you try Richards answer above? 👆 – asbjornenge Mar 26 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.