[英]How to visualize specific object keys in pug?
from this request:从这个请求:
router.get('/weather', function(req, res, next) {
requestify.get('https://api.weatherbit.io/v2.0/current?city=Trenton,TN&key=myKey').then(function(response) {
// Get the response body (JSON parsed - JSON response or jQuery object in case of XML response)
console.log(response.getBody());
res.render('weather', {
message: (response.body)
})
});
I'm getting this JSON:我得到这个 JSON:
{
data: [
{
rh: 94,
pod: 'n',
lon: -88.94145,
pres: 998.7,
country_code: 'US',
clouds: 25,
ts: 1596444360,
solar_rad: 0,
state_code: 'TN',
city_name: 'Trenton',
wind_spd: 1.54,
wind_cdir_full: 'east-southeast',
wind_cdir: 'ESE',
slp: 1012.4,
vis: 5,
lat: 35.98062,
temp: 18.9,
station: 'F5468',
elev_angle: -22.32,
app_temp: 19.3
}
],
count: 1
}
Opening my browser and going to http://localhost:3000/weather
the resulting JSON is correctly visualized.打开我的浏览器并转到
http://localhost:3000/weather
,得到的 JSON 被正确显示。 However, I would like to visualize only only lat / long and temp by using pug.但是,我想通过使用哈巴狗只可视化纬度/经度和温度。
Here my pug code:这是我的哈巴狗代码:
html
head
title= Weather
body
h1= message
How to do that?怎么做?
If you want it all in h1
string you could do:如果你想把它全部放在
h1
字符串中,你可以这样做:
html
head
title= Weather
body
h1= `${message.lon}, ${message.lat}, ${message.temp}` // or any other way you'd concat strings in javascript.
OR或者
html
head
title= Weather
body
h1= message.lon
h1= message.lat
h1= message.temp
you can also send partial data from your route:您还可以从您的路线发送部分数据:
res.render('weather', {
message: {let: response.body.let, lon: response.body.lon, temp: response.body.temp}
});
This is all considering you really are sending a parsed object otherwise you'd have to parse it somewhere along the road with JSON.parse()
这都是考虑到你真的在发送一个解析过的 object 否则你必须在路上的某个地方用
JSON.parse()
解析它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.