【问题标题】:How to get an image from gridFS in Meteorjs如何从 Meteorjs 中的 gridFS 获取图像
【发布时间】:2015-07-14 05:05:13
【问题描述】:

我在 MeteorJS 中使用 gridFS 包。

系统所做的是上传带有图片、标题、类别和价格的产品。这将保存在产品集合中。

使用模板助手,我可以从数据库中获取标题为 {{title}} 和价格为 {{price}} 的产品。但是我无法使用产品文档中的参考来获取图像。

谁能帮我解决这个问题?

    <template name="product">
        <div class="small-6 medium-3 columns end">
                <div class="product-container">
                    <div class="image-container">
                        {{#with image}}
                            <img src="{{this.url}}" class="product-image" alt="" />
                        {{/with}}
                    </div>
                    <div class="product-details">
                        <div class="product-title">
                            <a href="/productpage">{{title}}</a>
                        </div>
                        <div class="product-price">
                            {{price}}
                        </div>
                        <div class="product-buttons">
                            <span class="icon-heart"></span>
                            <span class="icon-repost"></span>
                            <span class="icon-plus"></span>
                        </div>
                    </div>
                </div>
            </div>
    </template>


    <template name="home">
    {{#each products}}
        {{> product}}
    {{/each}}
    </template>


    Products = new Mongo.Collection("products");

        var productImagesStore = new FS.Store.FileSystem('productImages', {
            path: '~/uploads/full'
        });

        productImages = new FS.Collection('productImages', {
            stores: [productImagesStore]
        });
        productImages.allow({
            insert: function () {
                return true;
            },
            update: function () {
                return true;
            },
            remove: function () {
                return true;
            },
            download: function () {
                return true;
            }
        });


    if (Meteor.isClient) {
          // This code only runs on the client
          Template.home.helpers({
            products: function () {
                return Products.find({});
            },
            image:function(){
                return productImages.findOne({'metadata.productId':this._id})
            }
          });

          Template.add.helpers({
            categories: function(){
                return categories.find({});
            }
          });


        Template.add.events({
            "submit form": function(event, template) {
                event.preventDefault();

                var file = $('#file').get(0).files[0],
                fsFile = new FS.File(file),
                productData = {
                    title:$('#title').val(),
                    price:$('#price').val(),
                    category:$('#category').val()
                }

                Products.insert(productData,function(err,result){
                    if(!err){
                        fsFile.metadata = {
                            productId:result, //we use metadata to refer the recent object id with the product images
                        }
                        productImages.insert(fsFile,function(err,result){
                            if(!err){
                                console.log("New images inserted")
                            }
                        });
                    }
                });
            }
        });
    }

【问题讨论】:

    标签: mongodb meteor gridfs


    【解决方案1】:

    您可以像这样将引用存储在 productImages 集合中。

     var file = $('#file').get(0).files[0],
         fsFile = new FS.File(file),
         productData = {
           title:$('#title').val(),
           price:$('#price').val(),
           category:$('#category').val()
                    }
        Products.insert(productData,function(err,result){
                   if(!err){
                     fsFile.metadata = {
                            productId:result, //we use metadata to refer the recent object id with the product images
                        }
                     productImages.insert(fsFile,function(err,result){
                      if(!err){
                         console.log("New images inserted")
                        }
                      });
                     }
                   });
    

    这是相同的插入,但更干净一点

    现在您可以像这样使用帮助程序和 this 上下文。

    HTML

    {{#each products}}
       {{title}}
       {{price}}
        {{#with image}}
          IMG URL:{{this.url}}
          <img src="{{this.url}}" />
        {{/with}}
     {{/each}}
    

    JS(助手)

    Template.example.helpers({
      products:function(){
       return Products.find()
      },
       image:function(){
       return productImages.findOne({'metadata.productId':this._id})
       }
    })
    

    【讨论】:

    • 嘿,非常感谢。但它仍然不起作用。我没有收到任何错误。我已经更新了我的代码。我发现我同时安装了 gridFS 和 cfs:filesystem,这有关系吗?
    • 您不确定图像是否已插入并保存在 ~/uploads/full 路径中?如果你运行 productImages.fetch() 你会得到什么?
    • 是的,我检查了文件系统,图像存储在正确的路径中。我在哪里运行它? (对 JS 和流星来说很新)我已经在控制台中运行了它,但是运行错误。
    • 在控制台上运行 productImages.find().fetch();如果你得到类似“productImages”的东西不存在,你是否删除了不安全的包?如果有,你有发布/订阅功能吗?
    • 当我运行时,我得到一个带有 FS.File 的数组
    猜你喜欢
    • 2012-01-30
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    相关资源
    最近更新 更多