【问题标题】:Meteor how to use an array from mongodb collection流星如何使用来自 mongodb 集合的数组
【发布时间】:2015-07-07 09:09:52
【问题描述】:

尝试使用具有数组的 Mongodb 集合。想在模板中使用数组。

Index.html

<body>
{{>test}}
</body>
<template name="test">
{{ #each task}}
 <p>{{this}}</p>
 {{/each}}
 </template>

The app.js

Task= new Mongo.Collection("Tasks");

if (Meteor.isClient) {
  // This code only runs on the client
  Template.test.helpers({
    task: function () {
      return Tasks.find({"cat":"TASK"}, {"_id":0, "ALL_TASKS":1});
    }
  });
}

if (Meteor.isServer){

    if (Task.find({}).count() === 0){
      Task.insert({"cat":"TASK", "ALL_TASKS":["t1","t2"]})
    }

}

它不起作用。我错过了什么

【问题讨论】:

    标签: arrays mongodb templates meteor


    【解决方案1】:

    您应该多使用一个 {{#each}} 。您正在使用的 {{#each}} 返回集合对象,而不是实际想要的数组。试试这个:

     <template name="test">
       {{#each task}}
           {{#each this.ALL_TASKS}}
             <p>{{this}}</p>
           {{/each
       {{/each}}
     </template>
    

    请注意,this.ALL_TASKS 现在正在遍历集合对象内部的数组。

    你的助手应该是这样的:

    if (Meteor.isClient) {
     // This code only runs on the client
      Template.test.helpers({
       task: function () {
        return Tasks.find({"cat":"TASK"});
       }
     });
    }
    

    【讨论】:

      猜你喜欢
      • 2016-05-10
      • 2018-02-12
      • 2013-04-26
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多