显示来自Meteor 1.0的数据

时间:2014-11-02 20:25:00

标签: javascript node.js mongodb meteor

启动时,我会从json文件中向meteor添加数据,如下所示:

 13 if (Meteor.isServer) {
 14
 15   //startup
 16   Meteor.startup(function () {
 17     if(Plugins.find().count() === 0) {
 18       var plugins_data = JSON.parse(Assets.getText('plugins_data.json'));
 19       _.each(plugins_data, function(){
 20         Plugins.insert( plugins_data );
 21         console.log('created new plugin record');
 22       });
 23     }
 24   });
 25
 26 }

我有一个名为plugins的集合,看起来像是使用db.plugins.find().pretty()中的meteor mongo来查看数据:

"222" : {
    "plugin-name-one" : {
        "data" : [
            {
                "id" : 888,
                "title" : ""
            }
        ]
    }
},
"223" : {
    "plugin-name-two" : {
        "data" : [
            {
                "id" : 555,
                "title" : ""
            }
        ]
    }
},

这里我是如何尝试显示数据的:

  3 if (Meteor.isClient) {
  4
  5   Template.list.helpers({
  6     list_all: function() {
  7       return Plugins.find();
  8     }
  9   });
 10
 11 }

和html模板:

  5 <body>
  6   <h1>Welcome to Meteor!</h1>
  7     {{> list}}
  8 </body>
  9
 10
 11 <template name='list'>
 12   {{#each list_all}}
 13     <h1>{{name}}</h1>
 14   {{/each}}
 15 </template>

如何从我的mongo集合中显示来自字段(和字段名称)plugin-name-1plugin-name-2的数据?我找不到文档中的任何信息。如何正确显示此数据?

1 个答案:

答案 0 :(得分:1)

您没有任何名为name的游泳池。

此外,检查是否有创建集合的更好方法是使用if(Plugins.findOne())

更重要的是,ENUMERATE_THIS是一个对象,因此如果我没记错,重命名{{name}}后会返回[Object object]

编辑: 编辑后确定我想我知道你想做什么,IMO结构应该是这样的

{
        name:"plugin_one",
        "data" : [
            {
                "id" : 888,
                "title" : ""
            }
        ]
},
 {
        name:"plugin_two",
        "data" : [
            {
                "id" : 888,
                "title" : ""
            }
        ]
},

我假设您想在一个插件中保留更多数据,这就是为什么有一个数组,如果没有结构就可以这样的

{
        name:"plugin_one",
        id : "888",
        title : ""
},
 {
        name:"plugin_two",
        id : "888",
        title : ""
},

在HTML中,您可以执行以下操作:

<template name='list'>
    {{#each list_all}}
      <h1>{{name}} {{data.id}}</h1>
    {{/each}}
</template>

或没有数组

<template name='list'>
        {{#each list_all}}
          <h1>{{name}} {{id}}</h1>
        {{/each}}
    </template>