我正在使用 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/