我得到了一个错误,而做js计算



const api_url = 'https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USD'
async function getprice() {
const response = await fetch(api_url);
const data = await response.json();
console.log(data);
return parseFloat(data) // we return the price as float
}
const calculate = async () => {
const price = await getPrice() // we call price function and save it in price variable 
const result = price * balance      
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<spam id="price">-</spam> // here to be output
</body>
</html>

我想从这个URL得到BTC的价格,然后乘以我钱包里的余额代码是这个

const api_url = 'https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USD'
async function getprice() {
const response = await fetch(api_url);
const data = await response.json();
console.log(data);
// document.getElementById('data').textContent = JSON.stringify(data.USD)
}
getprice();
let x = {data} * {balance}

但是我得到一个错误。

不确定这是否能回答你的问题,因为你提供的信息不够,所以我们无法重现你的问题。

编辑:

与你提供的片段我附加你一个工作片段,我不知道你在哪里得到你的平衡,所以我用了一个随机数为例。另外,spam元素有一个错别字,它必须是span。希望对你有帮助。

const api_url = 'https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USD'

const getPrice = async () => {
const response = await fetch(api_url);
const data = await response.json();
return parseFloat(data.USD) // we return the price as float
}

const balance = 53 // IDK where are you getting this value I just used a random number as example
const calculate = async () => {
const price = await getPrice() // we call price function and save it in price variable 
const result = price * balance
document.querySelector('#price').innerText = result; // Log into span element
}

calculate();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<span id="price">-</span> // here to be output
</body>
</html>

相关内容

最新更新