Handlebars hides properties from Mongoose documents in your template
Access has been denied to resolve the property "from" because it is not an "own property" of its parent.Short answer
Since Handlebars 4.6, templates cannot read properties that live on an object's prototype. A Mongoose document keeps its fields behind getters on the prototype, so they are blocked. Pass plain objects with .lean(), or allow only the named properties through runtime options. Avoid the allow-everything switch.
Fix it fast
1Find out which cause you have
Check whether the value you pass to res.render is a Mongoose document or a plain object. A document is the usual trigger.
const doc = await Message.findOne(); console.log(doc instanceof mongoose.Document); // true means a hydrated document2Match what you see, then jump to its fix
What does the error look like?
Access has been denied to resolve the property "from" because it is not an "own property" of its parent.The template renders but the values are empty
Cause 1 of 2
You render Mongoose documents instead of plain objects
1Check
A normal Mongoose result is a hydrated document. A lean result is a plain object, so its fields are own properties that Handlebars can read.
const normal = await Message.findOne();
const lean = await Message.findOne().lean();
console.log(normal instanceof mongoose.Document, lean instanceof mongoose.Document);2Fix
Add lean() to the query that feeds the view. Note that lean results have no virtuals, custom getters or save().
app.get('/', async (req, res) => {
const messages = await Message.find().lean();
res.render('home', { messages });
});3Verify
Reload the page. The values appear and the warning is gone from the console.
curl -s http://localhost:3000/ | head -n 20Confirmed by mongoosejs.com
Cause 2 of 2
The property really lives on the prototype and you need it
1Check
Handlebars blocks non-method prototype properties by default. The runtime option allowedProtoProperties lets named properties through, and the docs warn that allowing prototype properties may let template authors execute arbitrary code.
grep -n "allowedProto" src/2Fix
Allow only the exact property names your templates use. With express-handlebars, set them in runtimeOptions.
const hbs = create({
runtimeOptions: {
allowedProtoProperties: { from: true, subject: true },
},
});
app.engine('handlebars', hbs.engine);3Verify
Restart and open the page. Only the listed fields render, other prototype properties stay blocked.
node server.jsConfirmed by handlebarsjs.com
How do I stop it coming back?
- Pass lean() results or small view-model objects to templates, not whole documents.
- Never set allowProtoPropertiesByDefault to true for templates users can edit.
- Pin the property names you allow, and review the list when the schema changes.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fixBack end
bcryptjs rejects hash() or compare() with Illegal arguments
Error: Illegal arguments: string, undefined
bcryptjs checks the types of its two arguments and, when either is wrong, throws Illegal arguments followed by the typeof of the first and the second.
3 causes, each with a fixBack end
Express warns 'body-parser deprecated undefined extended'
body-parser deprecated undefined extended
The warning appears because urlencoded() was called without the extended option, which body-parser 1.x has warned about since version 1.4.0.
3 causes, each with a fixBack end
Browser blocks a stylesheet or script: MIME type mismatch with nosniff
The Network tab shows the asset URL answered with Content-Type text/html
With X-Content-Type-Options: nosniff the browser accepts a stylesheet only as text/css and a script only as a JavaScript type.
3 causes, each with a fix