Same name for property and helper


I have a Collection that includes a property of foo. However, foo is also a Collection Helper method.

i.e. const CollectionSchema = new SimpleSchema({ foo: { type: String }

and then

    foo: function () { return someLogic }

My problem is that I call Note that I leave out the () to denote I’m trying to access the property and not call the method i.e.

This works fine if foo exists on the Document. Otherwise I get

ƒ () length: 0 name: "foo" arguments: null caller: null prototype: {constructor: ƒ} __proto__: ƒ () [[FunctionLocation]]: file.js L123 [[Scopes]]: Scopes[2]

Looks like it’s returning a reference to the function foo Helper itself.

Does anybody know what’s going on and how I can resolve this? I acknowledge I can simply rename the foo Helper, but just curious


All property lookups in Javascript search up the prototype chain until a value is found:

Ah, another perfect answer. Thanks once again, Fred. Looks like I’ll have to rename the helper

If it’s not too big a change for your project, I’d suggest using static helpers instead.

So instead of wrapping documents with a class/prototype with methods on each document, you pass the document into the methods of a separate class:

// Instead of
const document = Collection.findOne({ foo: "bar" });
// Use a separate class
import { BarHelpers } from '/api/bar';
const document = Bar.findOne({ foo: "bar" });;

The separation of concerns makes it easier to reason about your documents (they are just the data) and test the helpers in isolation.
Also never have to worry about naming clashes

Please note that this is my personal preference and is not prescriptive. Just advising on what I do differently that avoids the original issue

1 Like

Ah! Very neat! Haven’t thought about static classes since playing with Java, but cool little workaround that keeps things abstracted too.

Turns out I was able to just completely remove the helper and solve the clash. But I’ll keep your methodology in mind.

Much appreciated!