CollectionFS从客户

时间:2015-10-17 15:37:53

标签: javascript meteor gridfs spacebars collectionfs

我正在开发我的第一个流星应用程序,并且刚刚使用CollectionFS和gridfs实现了上传图像。我可以使用<img src="{{this.url}}">成功查看我上传的图像,但我需要从Meteor中的客户端访问它。我正在尝试使用集合中另一个文档的字段,并根据元数据中的字段是否匹配来调用图像。 (基本上使用Spacebars registerHelper连接这两个文档)。我对于将其他文件嵌入到图像中犹豫不决,因为我担心空间要求。

我试过浏览Meteor-cfs-ui包,但是在将它翻译成我自己的Spacebar registerHelper时遇到了麻烦。浏览Meteor-CollectionFS上的UI Helpers页面,找到了我认为我所在的答案所在的链接。

我可以查看我上传的所有图片,因此使用{{this.url}}不是问题。只是尝试使用客户端FS.Collection上的查询获取集合中图像的URL令我感到不安。

这是我的代码:

client.html

    {{#each individualBuyOffers}}
    <div class="col-sm-6 col-md-4">
        <div class="thumbnail">

            <h4>{{shoePict make}}</h4>

        </div>
    </div>
    {{/each}}

client.js

      UI.registerHelper('shoePict',function(context,options){

         if(context){
             //The goal is to query the Pictures collection
             //and find the url to the picture I want
             //then output it to HTML from the SafeString
             //Pictures is my FS.Collection
             var url = Pictures.find({'metadata.make': context});
             //var collection = FS._collections[Pictures];
             //return collection ? collection.findOne(id) : null;
             //I know this is not the right statement
             return new Spacebars.SafeString( "<img src=\"{{this.url}}\">") ;
             }
      });

如果有任何其他更简单的解决方案通过html将图片从CollectionFS附加到另一个文档,我很乐意听到它。我只是想学习。感谢。

1 个答案:

答案 0 :(得分:1)

从js,您可以使用fileObj.url()访问collectionFS文件的网址,例如:

var url = Pictures.findOne({'metadata.make': context}).url();
相关问题