Проблема
Когда вы используете for-each в Handlebars, вам нужно перебирать массив / список.
Но ваш json (см. Ниже) не содержит никаких массивов / списков.
{
"Meta Data": {
"1. Information": "Daily Time Series with Splits and Dividend Events",
"2. Symbol": "MSFT",
"3. Last Refreshed": "2020-02-25 11:20:58",
"4. Output Size": "Full size",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2020-02-25": {
"1. open": "174.2000",
"2. high": "174.8400",
"3. low": "169.8800",
"4. close": "170.4300",
"5. adjusted close": "170.4300",
"6. volume": "16881624",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0000"
},
"2020-02-24": {
"1. open": "167.7700",
"2. high": "174.5500",
"3. low": "163.2300",
"4. close": "170.8900",
"5. adjusted close": "170.8900",
"6. volume": "67892482",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0000"
}
}
Решение
Ниже json, который имеет массивы / списки.
[
{"Meta Data": [
{"1. Information": "Daily Time Series with Splits and Dividend Events"},
{"2. Symbol": "MSFT"},
{"3. Last Refreshed": "2020-02-25 11:20:58"},
{"4. Output Size": "Full size"},
{"5. Time Zone": "US/Eastern"}
]},
{"Time Series (Daily)": [
{ "2020-02-25": [
{"1. open": "174.2000"},
{"2. high": "174.8400"},
{"3. low": "169.8800"},
{"4. close": "170.4300"},
{"5. adjusted close": "170.4300"},
{"6. volume": "16881624"},
{"7. dividend amount": "0.0000"},
{"8. split coefficient": "1.0000"}
]},
{"2020-02-24": [
{"1. open": "167.7700"},
{"2. high": "174.5500"},
{"3. low": "163.2300"},
{"4. close": "170.8900"},
{"5. adjusted close": "170.8900"},
{"6. volume": "67892482"},
{"7. dividend amount": "0.0000"},
{"8. split coefficient": "1.0000"}
]}
]}
]
Итак, чтобы создать этот новый json, мы будем использовать <em>Node.js (JavaScript)</em>
в бэкэнде.
router.get('/stock/:name',(req,res)=>
{
let name = req.params.name;
console.log(name);
axios.get('https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=${name}&apikey=apikey').then((result)=>
{
const newData1 = result.data; // <-------- HERE, I will recommend 'const' instead of 'var' :)
const timeSeriesDaily = newData1["Time Series (Daily)"];
let timeSeriesDailyJSON = [] // <----- timeSeriesDailyJSON will be 'filled' in the for-loop below
for (let dateIndex in timeSeriesDaily) {
let array = timeSeriesDaily[dateIndex];
let newArray = [];
for(let index in array){
newArray.push({
index: index,
value: array[index]
})
}
timeSeriesDailyJSON.push({
date: dateIndex,
array: newArray
});
}
res.render('home/stock', {timeSeriesDailyJSON});
})
})
{{#each timeSeriesDailyJSON}}
<h5>{{date}}</h5>
{{#each array}}
<h5>{{index}}</h5>
<h5>{{value}}</h5>
{{/each}}
{{/each}}