javascript - Handlebars 过滤对象

标签 javascript node.js express handlebars.js

我正在使用 Express、NodeJS 和 MongoDB 。尝试使用 Handlebars 传递一些信息进行渲染。

我使用的路线代码是:

router.get('/mediciones', async (req, res) => {
  // res.render('mediciones');
  const dashboard  = await Mediciones.find().limit(3);
  console.log(dashboard);
  res.render('mediciones' , { dashboard });
});

hbs 代码是:

{{#each  dashboard }}
<p>
    {{!-- {{this}} --}}      // using just one, but both show me the 3 objects
    {{.}}
</p>
{{/each}}

渲染 View :

{ _id: 5ea9ff48b54a2b09d38ddfd9, temperatura: 20, humedad: 44, heat: 19.2, temperaturaagua1: 22.31, temperaturaagua2: 22.31, ventilador: true, extractor: true, intractor: true, bombaagua: true, refreshInterval: 10000, currentMillis: 3799956, dia: 2020-04-29T22:27:20.997Z, __v: 0 }

{ _id: 5ea9ff52b54a2b09d38ddfda, temperatura: 20, humedad: 44, heat: 19.2, temperaturaagua1: 22.31, temperaturaagua2: 22.31, ventilador: true, extractor: true, intractor: true, bombaagua: true, refreshInterval: 10000, currentMillis: 3809961, dia: 2020-04-29T22:27:30.986Z, __v: 0 }

{ _id: 5ea9ff5db54a2b09d38ddfdb, temperatura: 20, humedad: 44, heat: 19.2, temperaturaagua1: 22.31, temperaturaagua2: 22.31, ventilador: true, extractor: true, intractor: true, bombaagua: true, refreshInterval: 10000, currentMillis: 3820030, dia: 2020-04-29T22:27:41.068Z, __v: 0 }

问题是:我如何从这些对象中提取“temperatura”和“humedad”并将它们打印在 <p> 上标签?我尝试使用 {{temperatura}}但什么也没有显示。我找不到答案

最佳答案

try this.

const dashboard  = await Mediciones.find().limit(3).lean();

关于javascript - Handlebars 过滤对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61514727/

相关文章:

javascript - jQuery缩略图替换方法(需要添加A标签的解决方案)

javascript - 在 BODY 中调用许多函数

javascript - 关于将数据发布到expressjs的简单xmlhttprequest的问题

node.js - Nodejs Express-服务器的 get 方法获取 3 个参数

javascript - express.js 不响应 https 帖子

node.js - 使用 Mercurial 推送 Node.js 应用程序部署

JavaScript 本地应用程序保存/加载文件?

javascript - 用于 jQuery 输入验证的正则表达式

javascript - 无法使用非对象作为目标或处理程序创建代理

javascript - 将 gulp-sourcemaps 与 gulp-csso 和 gulp-sass 结合起来