Reputation: 356
Using Handlebars 4.0.6 and NodeJS 7.4.0, I output the data that my template is receiving using {{this}}
, which outputs:
{ _id: 58a7de1c7275f8208438ae4a,
author: 589a12b5a08e0c2f24ece4e8,
dateCreated: 2017-02-18T05:39:40.650Z,
section: 58a7d57c9ce34527bce7e041,
slug: 'this-one-will-work',
title: 'this one will work',
color: '#d313ff',
__v: 0,
fields:
[ { fieldId: 58a3cff51da0ea5d00972804,
fieldSlug: 'color',
value: '#d313ff',
_id: 58a7de1c7275f8208438ae4b } ] }
However, using {{color}}
or {{this.color}}
in the template right below the {{this}}
doesn't output anything.
I'm getting the data with mongoose in a Promise, then compiling it, the using res.send()
.
Here's my compiling function:
const Handlebars = require('handlebars');
const fs = require('fs');
const path = require('path');
module.exports = (template, data) =>
new Promise((resolve, reject) => {
const templateWithFormat = template.endsWith('.hbs') ? template : `${template}.hbs`;
const templatePath = path.resolve(__dirname, '..', '..', 'templates', templateWithFormat);
fs.readFile(templatePath, 'utf-8', (err, file) => {
if (err) reject(err);
const compiled = Handlebars.compile(file);
const html = compiled(data);
resolve(html);
});
});
Anyone have any ideas?
Thanks!
Upvotes: 0
Views: 325
Reputation: 4326
You might have a problem if you pass a mongoose model object to Handlebars. (because mongoose wraps an object and adds getters/setters for the properties)
To solve this issue please try running your query with {lean: true}
or call modelObj.toObject()
before sending it to handlebars.
Upvotes: 1