我希望这不是重复的,我发现了类似的问题,但没有一个能准确说明我的问题。
我在 Nomics API 工作,这是一种加密货币 API。这是我发出的 API 请求:https://api.nomics.com/v1/currencies/ticker?key=2018-09-demo-dont-deploy-b69315e440beb145&ids=BTC,ETH,LTC,XRP,DOGE
我正在尝试获取变量 ${coin.1d.price_change}...
但它是控制台记录错误:
未捕获的语法错误:模板表达式中缺少 }
这适用于非嵌套 JSON,例如 ${coin.price}...
我正在处理的代码是:
function getValueChanges() {
return fetch(proxyurl + "https://api.nomics.com/v1/currencies/ticker?key=2018-09-demo-dont-deploy-b69315e440beb145&ids=BTC,ETH,LTC,XRP,DOGE").then(function(response) {
return response.json();
}).then(function(json) {
return json;
});
}
getValueChanges().then(function(result) {
result.forEach(coin => {
const card = document.createElement('div');
card.setAttribute('class', 'card');
const h1 = document.createElement('h1');
h1.textContent = coin.currency;
const p = document.createElement('p');
//coin.description = coin.description.substring(0, 300);
// 🚨🚨!!!! ERROR IM TALKING ABOUT !!!!🚨🚨 //
p.textContent = `${coin.1d.price_change}...`;
// 🚨🚨!!!! ERROR IM TALKING ABOUT !!!!🚨🚨 //
container.appendChild(card);
card.appendChild(h1);
card.appendChild(p);
console.log(coin.currency + ": " + coin.price);
});
//console.log(result);
});
基本上我是用加密货币信息制作卡片,我对传递错误的语句进行了一些评论。
谢谢,如果我做错了什么(这是我的第一个问题) lmk,当我拿到下一份薪水时,我会请你喝咖啡。
最佳答案
Pointy 想通了,非常简单的错误
coin.1d 是语法错误。对于要与 .运算符,它们必须是有效的标识符,并且标识符不能以数字开头。
您可以改用 ${coin['1d'].price_change}
关于javascript - 嵌套的 JSON 调用给出 : Uncaught SyntaxError: Missing } in template expression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58066171/