最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - updating html from data retrieved in api call - Stack Overflow

programmeradmin0浏览0评论

I am new to web dev so please forgive.

I make simple api call

  const getWorldTotal = async () => {
  const response = await fetch('://health-api/api/v1/covid-19/total');
  const worldTotal = await response.json();
  alert(worldTotal.total_confirmed)
  document.getElementById('total') = worldTotal.total_confirmed
};
getWorldTotal()

Here is my html that i am trying to update

<div class="card text-white bg-secondary mb-3" id="total" style="max-width: 20rem;">
<div class="card-header">Header</div>
<div class="card-body">
  <h4 class="card-title" id="total"></h4>
</div>
</div>

I get the following error

index.html:80 Uncaught (in promise) ReferenceError: Invalid left-hand side in assignment at getWorldTotal

My question is what did i do wrong? Also is this the best way to update HTML when making api calls ?

I am new to web dev so please forgive.

I make simple api call

  const getWorldTotal = async () => {
  const response = await fetch('https://cors-anywhere.herokuapp./https://health-api./api/v1/covid-19/total');
  const worldTotal = await response.json();
  alert(worldTotal.total_confirmed)
  document.getElementById('total') = worldTotal.total_confirmed
};
getWorldTotal()

Here is my html that i am trying to update

<div class="card text-white bg-secondary mb-3" id="total" style="max-width: 20rem;">
<div class="card-header">Header</div>
<div class="card-body">
  <h4 class="card-title" id="total"></h4>
</div>
</div>

I get the following error

index.html:80 Uncaught (in promise) ReferenceError: Invalid left-hand side in assignment at getWorldTotal

My question is what did i do wrong? Also is this the best way to update HTML when making api calls ?

Share Improve this question edited Mar 23, 2020 at 21:14 Cerbrus 73.1k19 gold badges136 silver badges150 bronze badges asked Mar 18, 2020 at 4:07 DineroDinero 1,1604 gold badges25 silver badges53 bronze badges 2
  • first of all you added two urls in the string in fetch(), add any one at a time. – Sivaramakrishnan Commented Mar 18, 2020 at 4:15
  • document.getElementById('total').textContent = worldTotal.total_confirmed ==> add .textContent – Mister Jojo Commented Mar 18, 2020 at 4:16
Add a ment  | 

3 Answers 3

Reset to default 3

The Document method getElementById() returns an Element object and you are trying to change it which gives you the error.
Source . Also, if you want to change the text you can use innerText

  const getWorldTotal = async () => {
  const response = await fetch('https://cors-anywhere.herokuapp./https://health-api./api/v1/covid-19/total');
  const worldTotal = await response.json();
  alert(worldTotal.total_confirmed)
  document.getElementById('total').innerText = worldTotal.total_confirmed
};

getWorldTotal()

try this:

const xURL  = 'https://cors-anywhere.herokuapp./https://health-api./api/v1/covid-19/total'
  ,   total = document.getElementById('total')
  ;
const getWorldTotal = async () => {
  let data = await (await fetch(xURL)).json()
  //console.log( 'data ', data)  
  total.textContent = data.total_confirmed
}

getWorldTotal()

As Rajesh and Mr Jojo stated, I think adding ".textContent" or ".innerText" after "document.getElementById('total')" will help with this.

Also, when calling the function, you can add a semicolon to end the statement. getWorldTotal() + ";". While this is optional, it can be good to get in the habit of 'strict'.

发布评论

评论列表(0)

  1. 暂无评论