0

Java client application is inserting data into meteor's mongoDB. One of the parameters in json object being sent over to meteor is HashMap<String, MyObject> converted as gson.toJson(HashMap<String, MyObject>). When front end receives Document/Collection object, it's able to get this data block by name and it looks like this:

"RANDOM_NAME1":{"service":"RANDOM_NAME1","count":20,"maxCount":300},
"RANDOM_NAME2":{"service":"RANDOM_NAME2,"count":50,"maxCount":340},
"

I don't know what RANDOM_NAME could be, any string is possible, but value in curly brackets is always 3 parameters - service, count, and maxCount.

Here is my attempt to loop through this block of data to display 3 parameters separately:

HTML - nothing displayed

<template name="displayData">
<h2> Data </h2>
   {{#each top}}
     {{#with interested_block}}
           <p>"service: " {{service}} "count:" {{count}} "max: " {{maxCount}}</p>
     {{/with}}
    {{/each}}
</template>

HTML - test document where i know RANDOM_NAME value, still show nothing

<template name="displayData">
    <h2> Data </h2>
       {{#each top}}
         {{#with interested_block}}
               <p>{{RANDOM_NAME}} </p>
         {{/with}}
        {{/each}}
    </template>

HTML - i can display the whole block of data ( gson.toJson(HashMap<String, MyObject>) as above)

<template name="displayData">
<h2>  Data </h2>
   {{#each top}}
           <p>"java hashmap: " {{interested_block}}</p>
    {{/each}}
</template>

Is this conversion gson.toJson(HashMap<String, MyObject>) unparsable or there is another way of displaying besides what i've tried?

1 Answers1

0

Reason:

{{#each}} currently only accepts arrays, cursors or falsey values.

Solution:

Convert object to array (source Meteor and handlebars #each to iterate over object) :

Handlebars.registerHelper('arrayify',function(obj){
    result = [];
    for (var key in obj) result.push(obj[key]);
    return result;
});

Assuming you receive:

top = {
    "RANDOM_NAME1":{"service":"RANDOM_NAME1","count":20,"maxCount":300},
    "RANDOM_NAME2":{"service":"RANDOM_NAME2","count":50,"maxCount":340}
}

then usage :

<template name="displayData">
    <h2> Data </h2>
       {{#each arrayify top}}
         <p>"service: " {{service}} "count:" {{count}} "max: " {{maxCount}}</p>
       {{/each}}
</template>
Community
  • 1
  • 1
Kuba Wyrobek
  • 5,273
  • 1
  • 24
  • 26
  • Kuba, thanks so much, for your response, it doesn't work as expected. What happens is that result is an array of every single character of "top" object. It doesn't see it as key/value pair. Any suggestions on how to resolve it? I am almost there .... – user3140656 Jun 16 '14 at 16:44
  • When i debug this, i see that 'key' in arrayify functionn is 'undefined'. Maybe i am missing some libraries to parse this? – user3140656 Jun 16 '14 at 17:28
  • if **top** consist of `key/value` pairs, then **arrayify** returns array of `value` objects. – Kuba Wyrobek Jun 16 '14 at 18:08
  • if `key` is `undefined` then it means `top` object is not properly created. Debug `top` object. – Kuba Wyrobek Jun 16 '14 at 18:09
  • I can see **top** object, exactly as in my first post, maybe **key** ("RANDOM_NAME1")should not be in quotation marks? It seems as client formatting issue. – user3140656 Jun 16 '14 at 19:57
  • Kuba, i think my issue is data type that i send to `arrayify' function, it shows as String. When i hard code this data in the function itself, it reads properly and it displays as [object Object]. Not sure how to retrieve this block in proper data type. – user3140656 Jun 17 '14 at 16:34
  • Try JSON.parse(), it should parse JSON to js object. – Kuba Wyrobek Jun 17 '14 at 17:31