1

I am working on a project in meteor which has a mongo collection.

Let's call this collection "A".

Documents in "A" are of the following structure:

{ id:"XXX", name:"YYY", array: { "1": text1, "2": text2 } }

I also have a template to show this kind of document inside my html:

<input type="text" name={{name}} value={{array.$1}} />

The value in array.1 is not presented. My question is how do I present this value?

I've been googling it for a long time now, found nothing useful but this: Rendering MongoDB documents with Meteor

Which didn't really help. Also meteor documentation doesn't seem to have information about this kind of usage.

Community
  • 1
  • 1
amit levin
  • 183
  • 1
  • 2
  • 15
  • Defining a helper function was needed: Template..helpers: 'array1' -> A.findOne({_id:@_id}).array[1] Then inside the html just call the function: {{array1}} – amit levin Feb 06 '16 at 12:27

1 Answers1

1

Are you sure you wanted to use '$' sign? In JS it doesn't mean anything special.

For example:

var array = {"1": "text1", "2":"text2"};
console.log(array.$1);

Comes back with "undefined".

And this name "array" is confusing because its prototype is not equal to Array.prototype

Object.getPrototypeOf(array) === Array.prototype

comes back with "false".

The following code should work.

<input type="text" name={{name}} value={{array['1']}} />

Also check this out: JavaScript property access: dot notation vs. brackets?

Community
  • 1
  • 1
Lukasz K
  • 162
  • 2
  • 13