如何可视化哈巴狗中的特定对象键?

How to visualize specific object keys in 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 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 已正确显示。但是,我想通过使用哈巴狗只可视化纬度/经度和温度。

这是我的哈巴狗代码:


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
  head
    title= Weather
  body
    h1= message.lon
    h1= message.lat
    h1= message.temp

您还可以从您的路线发送部分数据:

res.render('weather', {
  message: {let: response.body.let, lon: response.body.lon, temp: response.body.temp}
});

这一切都考虑到您确实在发送一个已解析的对象,否则您必须使用 JSON.parse()

在路上的某个地方解析它