gpt4 book ai didi

javascript - 需要帮助消化使用express-handlebars node.js 返回的JSON blob

转载 作者:太空宇宙 更新时间:2023-11-03 22:28:41 25 4
gpt4 key购买 nike

我正在开始使用 node.js,并正在完成一个教程,该教程可 ping Accuweather API 并返回 JSON blob 数据。

我几乎明白了......但是显示部分阻碍了我:

index.js

const express = require('express')
const rp = require('request-promise')
const exphbs = require('express-handlebars')
const path = require('path')
const app = express()

app.engine('.hbs', exphbs({
defaultLayout: 'main',
extname: '.hbs',
layoutsDir: path.join(__dirname, 'views/layouts')
}))
app.set('view engine', '.hbs')
app.set('views', path.join(__dirname, 'views'))

app.get('/:city', (req, res) => {
rp({
uri: 'http://apidev.accuweather.com/locations/v1/search',
qs: {
q: req.params.city,
apiKey: 'hoArfRosT1215'
// Use your accuweather API key here
},
json: true
})
.then((data) => {
console.log(data)
res.render('home', data)
})
.catch((err) => {
console.log(err)
res.render('error')
})
})

app.listen(3000)

home.hbs

  <h2>Success!</h2>
<h2>{{data}}</h2>`

错误.hbs

 <h2>Error<h2>

home.hbs

<html>
<head>
<title>Express handlebars</title>
</head>
<body>
{{{body}}}
</body>
</html>

我用 Google 搜索过,但没有真正找到很好的解决方案。我研究了 Handlebars 的帮助功能......但并没有真正想出任何东西。

我如何开始显示从 Accuweather API 返回的一些“数据” block ?

仅供引用,这是我从 console.logging 返回的 JSON 博客

[ { Version: 1,
Key: '2156696',
Type: 'City',
Rank: 385,
LocalizedName: 'Providence Forge',
EnglishName: 'Providence Forge',
PrimaryPostalCode: '19468',
Region:
{ ID: 'NAM',
LocalizedName: 'North America',
EnglishName: 'North America' },
Country:
{ ID: 'US',
LocalizedName: 'United States',
EnglishName: 'United States' },
AdministrativeArea:
{ ID: 'PA',
LocalizedName: 'Pennsylvania',
EnglishName: 'Pennsylvania',
Level: 1,
LocalizedType: 'State',
EnglishType: 'State',
CountryID: 'US' },
TimeZone:
{ Code: 'EST',
Name: 'America/New_York',
GmtOffset: -5,
IsDaylightSaving: false,
NextOffsetChange: '2017-03-12T07:00:00Z' },
GeoPosition: { Latitude: 40.18, Longitude: -75.523, Elevation: [Object] },
IsAlias: false,
SupplementalAdminAreas: [ [Object] ],
DataSets: [ 'Alerts', 'ForecastConfidence', 'MinuteCast' ] },
{ Version: 1,
Key: '2172276',
Type: 'City',
Rank: 385,
LocalizedName: 'Providence Forge',
EnglishName: 'Providence Forge',
PrimaryPostalCode: '23140',
Region:
{ ID: 'NAM',
LocalizedName: 'North America',
EnglishName: 'North America' },
Country:
{ ID: 'US',
LocalizedName: 'United States',
EnglishName: 'United States' },
AdministrativeArea:
{ ID: 'VA',
LocalizedName: 'Virginia',
EnglishName: 'Virginia',
Level: 1,
LocalizedType: 'State',
EnglishType: 'State',
CountryID: 'US' },
TimeZone:
{ Code: 'EST',
Name: 'America/New_York',
GmtOffset: -5,
IsDaylightSaving: false,
NextOffsetChange: '2017-03-12T07:00:00Z' },
GeoPosition: { Latitude: 37.442, Longitude: -77.044, Elevation: [Object] },
IsAlias: false,
SupplementalAdminAreas: [ [Object] ],
DataSets: [ 'Alerts', 'ForecastConfidence', 'MinuteCast' ] } ]

最佳答案

好的,所以你有:

 res.render('home', data)

我认为应该是:

res.render("home", {data:data})

至于你的html

  {{#each data}}
<div>
Name: {{this.LocalizedName}}
Rank: {{this.Rank}}
</div>
{{/each}}

关于javascript - 需要帮助消化使用express-handlebars node.js 返回的JSON blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41404620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com