Iterating array-elements as single values inside collection-document

Hi,

I’m trying to create an online-list for a chat. Whenever a person moves into a room, his nickname gets pushed into the array inRoom, when he leaves it gets pulled out again. This works fine so far. Displaying the arrays on the actual list confuses me though.

A room-document looks like this:

{
  "_id": "crsEES5d22uYJiqDZ",
  "name": "Room",
  "roomdesc": " ",
  "inRoom": [name1, name2, name3],
  "createdAt": "2019-06-08T12:39:51.382Z",
  "owner": "Kuroki"
}

I have a helper to get the array. I use getParam to get the name of the room in order to find the right one.

Template.onlineliste.helpers({
  roomData() {
    var name = FlowRouter.getParam('name');
    return Channels.find({name:name}, {sort: {createdAt: 1}})
  },
});

Then I tried to iterate which of course will show the array as a whole and not every name as single value

<template name="onlineliste">
    {{#each roomData}}
    {{inRoom}} 
    {{/each}}
  </template>

Could someone show me how to iterate the single values of the array so they will display like

  • Name 1
  • Name 2
  • Name 3

instead of Name1, Name 2, Name 3

Thank you

http://blazejs.org/guide/spacebars.html#Each-in

A bit of an edge case that you might want to think about in your design is what happens when the user opens multiple windows of the same chat and then closes one. Currently my guess is thay it will not list them as being in the room.

I’m not completely clear about that {{each in}}-helper. I’m trying to understand it by imagining the collection-document of that example.

{{#each todo in todos}}
  {{#each tag in todo.tags}}
    <!-- in here, both todo and tag are in scope -->
  {{/each}}
{{/each}}

So in that case tag would be value of the array? Why is there a plural and singular form of todos? Todo would be the helper word, I guess? Like mine is roomData? I tried to make it like so:

{{#each roomData in roomDatas}}
  {{#each inRoom in roomData.inRoom}}
  {{roomData.inRoom}}
  {{/each}}
{{/each}}

But this displays nothing.

@copleykj

Right now, when someone opens another tab and joins the same room his name gets duplicated. If he closes one both values of his name get deleted. This is an issue I will work on.

If roomDatas is an array that you want to iterate, and when you pass it through #each. It iterates through each value/object in array. Earlier one used ‘this’ for the value/object in scope during iteration loop. Now it gets assigned to a variable in this case(your example) to roomData.
This is helpful when you need to use #each with nested arrays.

Try this:

{{#each roomData in roomDatas}}
  {{#each inRoom in roomData.inRoom}}
    {{inRoom}}
  {{/each}}
{{/each}}

Sadly I tried that already and it didn’t display anything without any errors.