Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Handlebars hides properties from Mongoose documents in your template

ErrorAccess 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.

  • Applies to Handlebars 4.7, express-handlebars 8, Mongoose 8, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 document
  2. 2Match what you see, then jump to its fix

    1. 1The property named in the warning is a schema field such as from or titleYou render Mongoose documents instead of plain objectsFix
    2. 2The warning stays after lean(), for a class instance or a virtual you cannot dropThe property really lives on the prototype and you need itFix

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 20

Confirmed by mongoosejs.com

Did this fix it?

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.js

Confirmed by handlebarsjs.com

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.