3

I have an array of strings of userId's and iterate through in a template with {{#each}}. However, #each seems to want an object and converts each string into and object like so:

String {0: "T", 1: "e", 2: "2", 3: "s", 4: "y", 5: "D", 6: "g", 7: "G", 8: "d", 9: "K", 10: "e", 11: "i", 12: "i", 13: "Q", 14: "S", 15: "i", 16: "W"} 

Pretty annoying. Any ideas how to avoid this issue?

Thanks

2
  • Why not just iterate through the user objects? Commented Dec 12, 2013 at 9:46
  • The problem is that JavaScript wants this to be an object. Commented Dec 12, 2013 at 16:26

4 Answers 4

4

How about

this.toString()

Might be more robust than String(this).

Sign up to request clarification or add additional context in comments.

Comments

1

I had the same problem, and

this.toString()

fixed the problem, but as someone new to Meteor I am curious why this is happening and if this is an acceptable solution for the problem.

Data

client {
    exampleProerty: value,
    projectManagers: [
        userId,
        userId 
    ],
    examleTwo: value
}

Template

{{#each projectManagers}}
    {{> userTemplate}}
{{/each}}

Template Helper

Template.userTemplate.helpers({

user: function() {
    var id = this.toString();
    var user = Meteor.users.findOne({_id: id});
    return user;
}

});

Otherwise, hoped this helped someone else.

1 Comment

This is not a good answer! You're querying the database from your view, adding an additional dependency to your storage. Try to get all information before start rendering.
1

For example, to display a list of items from an array:

// input to template
const shopping_list = [
  'Apples',
  'Oranges',
  'Bread',
  'Peanut Butter'
];

// template.hbs
<ul>
  {{#each shopping_list}}
    <li>{{this}}</li>
  {{/each}}
</ul>

Expected result:

  • Apples
  • Oranges
  • Bread
  • Peanut Butter

Comments

0

This seems to work:

String(this)

pretty inelegant having to do that though...

2 Comments

Also, keep in mind that with this constructor you don't get "real" strings, but string objects. They behave mostly the same, but some common assumptions are broken. For example, typeof "asdf" === "string", but typeof String("asdf") === "object".
I've just checked the above statement and it appears that typeof String("asdf") === "string" in current Firefox. So that problem is browser dependent - even funnier to debug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.