无法在 CollectionFS for MeteorJS 应用程序中操作 Images.find()



我的应用程序有点像TelescopeJS,但要简单得多。我试图呼应帖子添加表单中添加的特定图像,该表单输入了帖子的名称、图片、类别和描述。它有两个集合,一个用于Articles,另一个用于Images(不是mongo集合,它是一个FS集合。)Articles集合存储名称、描述和类别名称,而另一个存储图像**我的问题是:**在FS收集文档中,循环

{{#each images}}
    <img src="{{this.url}}" alt="" class="thumbnail" />
{{/each}}

其中images:返回images.find({})和我的文章代码是:

{{#each articles}}
    <li style="margin-right: 1%;">{{>article}}</li>  
{{/each}}

Where articles:返回articles.find({})

我的文章模板有图像循环,这导致集合中的所有图像都显示在一个帖子中。我只想为特定的帖子显示特定的图片。

以下是事件:

'change .img': function(event, template) {
    FS.Utility.eachFile(event, function(file) {
      Images.insert(file, function (err, fileObj) {
        //Inserted new doc with ID fileObj._id, and kicked off the data upload using HTTP
      });
    });
  },
    'click .save':function(evt,tmpl){
        var description = tmpl.find('.description').value;
        var name = tmpl.find('.name').value;
        var date=new Date();
        var cat = tmpl.find('.selectCat').value;
        Articles.insert({
            description:description,
            name:name,
            time:date.toLocaleDateString()+' at '+date.toLocaleTimeString(),
            author:Meteor.userId(),
            userEmail:Meteor.user().username,
            category:cat,
        });
    }
   <template name="article">
     {{#each images}}
        <img src="{{this.url}}" alt="" class="thumbnail" />
        {{/each}}
    Here goes the {{name_of_post}}
    Here {{the_category}}
    Here {{the_description}}
    </template>

到目前为止,我上传的所有图片都显示在一个帖子中,所有帖子的图片看起来都一样。救命!

您应该知道fsFile支持元数据,所以您可能不需要Articles Collection

所以我们可以制作一个新的CCD_ 2。

'click .save':function(evt,tmpl){
        var description = tmpl.find('.description').value,
            file = $('#uploadImagePost').get(0).files[0], //here we store the current file on the <input type="file">
            name = tmpl.find('.name').value,
            date=new Date(),
            cat = tmpl.find('.selectCat').value,
            fsFile = new FS.File(file); // we create an FS.File instance based on our file
          fsFile.metadata = {  //this is how we add Metadata aka Text to our files
                   description:description,
                   name:name,
                   time:date.toLocaleDateString()+' at '+date.toLocaleTimeString(),
                  author:Meteor.userId(),
                  userEmail:Meteor.user().username,
                  category:cat, 
                      }
          Images.insert(fsFile,function(err,result){
          if(!err){
            console.log(result) // here you should see the new fsFile instance
            }
       });
    }

这就是我们的新活动的外观,现在我们的.save按钮将所有内容插入同一集合中。

这就是我们使用关键字'metadata.fieldName'访问FS.File实例字段的方法。

例如。

Teamplate.name.helpers({
  showCategory:function(){
    // var category = Session.get('currentCategory') you can pass whatever data 
    // you want here from a select on the html or whatever.
   //lets say our var its equal to 'Music'
    return Images.find({'metadata.category':category});
  } 
})

现在,我们在html上使用该助手,就像任何正常的集合一样

<template name="example">
  {{#each showCategory}}
    Hi my category is {{metadata.category}} <!-- we access the metadata fields like any normal field on other collection just remember to use the 'metadata'keyword -->
    This is my image <img src="{{this.url}}" >
  {{/each}}
</template>

最新更新