Javascript 如何在pug中可视化特定对象关键点?
根据这项请求:Javascript 如何在pug中可视化特定对象关键点?,javascript,arrays,pug,Javascript,Arrays,Pug,根据这项请求: 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
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)
})
});
我得到了这个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
}
打开我的浏览器并转到http://localhost:3000/weather
生成的JSON正确可视化。但是,我只想通过使用pug来可视化lat/long和temp
这是我的哈巴狗代码:
html
head
title= Weather
body
h1= message
如何做到这一点?如果您想在
h1
字符串中实现这一点,您可以执行以下操作:
html
head
title= Weather
body
h1= `${message.lon}, ${message.lat}, ${message.temp}` // or any other way you'd concat strings in javascript.
或
html
头
标题=天气
身体
h1=message.lon
h1=message.lat
h1=message.temp
您还可以从路由发送部分数据:
res.render('weather'){
消息:{let:response.body.let,lon:response.body.lon,temp:response.body.temp}
});
这一切都是考虑到您确实在发送一个已解析的对象,否则您必须使用JSON.parse()