【问题标题】:Iterate through MongoDB collection with Meteor.js使用 Meteor.js 遍历 MongoDB 集合
【发布时间】:2015-12-18 18:00:23
【问题描述】:

我正在学习 Meteor,并且正在构建一个非常基本的应用程序以用于练习目的。 我刚刚安装了 Collection FS,我正在使用 cfs:filesystem 上传文件。 我有一个名为 todos 女巫的集合,其中包含以下字段:

"files" : [ "/cfs/files/files/HPxaJrEbvNjcnmA6m/files?store=files", "/cfs/files/files/jfKua2BszyfqiKYYu/files?store=files" ]

插入是通过服务器端的方法完成的:

'addFiles':function(id,url){
  todos.update(id, {
    $push: {files:url}
  });

如何遍历这些以将它们放入模板中的 url?

我在模板中尝试的是:

{{#each todos}}
{{#each files}}<a href="{{files}}" target="_blank"> file</a>{{/each}}
{{/each}}

但这只会渲染&lt;a target="_blank"&gt; file&lt;/a&gt; 如果文件对象没有任何值,我不想显示任何 url。 我该怎么做?

【问题讨论】:

    标签: mongodb meteor


    【解决方案1】:

    我假设您正确订阅了todos 集合并且有助手:

    Template.NAME.helpers({
        todos : function(){
           return todos.find();
        }
    })
    

    在模板名称中试试这个:

    {{#each todos}}
       {{#each this.files}}
         <a href="{{this}}" target="_blank"> file</a>
       {{/each}}
    {{/each}}
    

    如果files 为空,则不会显示任何内容。

    【讨论】:

    • 太棒了!只要允许,我就会接受你的回答。非常感谢!我不知道 {{this}} 属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 2014-08-04
    • 1970-01-01
    • 2014-04-11
    • 2014-11-08
    • 2017-11-20
    相关资源
    最近更新 更多