【问题标题】:collections2 and meteor-cfs-autoform to upload and display imagecollections2 和 meteor-cfs-autoform 上传和显示图像
【发布时间】:2016-01-26 17:03:10
【问题描述】:

我正在使用collections2和meteor-cfs-autoform作为模式,并使用cfs:gridfs存储适配器包上传和显示图像,但无法在模板中显示图像。你能告诉我我在哪里做错了及其解决方案。

collections.js

Recipes = new Mongo.Collection('recipes');
Reviews = new Mongo.Collection('reviews');
RecipesImages = new FS.Collection("recipesImages", {
    stores: [new FS.Store.GridFS("recipesImages")]
});
RecipesImages.allow({
    insert: function(userId, doc) {
        return true;
    },
    update: function(userId, doc, fieldNames, modifier) {
        return true;
    },
    download: function(userId) {
        return true;
    },
    fetch: null
});

Recipes.attachSchema(new SimpleSchema({
    name: {
        type: String,
        label: "Recipe Name",
        max: 100
    },

        ingredients: {
            type: [Object],
            minCount: 1
        },

    "ingredients.$.name": {
    type: String
        },
    "ingredients.$.amount": {
    type: String
    },
    description: {
        type: String,
        label: "How to prepare ",
    },
    time: {
        type: Number,
        label: "Time (Minutes)",
    },
    image: {
        type: String,
        autoform: {
            afFieldInput: {
                type: "cfs-file",
                collection: 'recipesImages',
                label: 'Recipe Picture'
            }
        }
    }
}));

recipes.js

   Template.recipes.helpers({
        'showRecipes':function(){
            return Recipes.find();
        },
        'images': function () {
            return RecipesImages.findOne({_id:id}) ;
        }
    })

recipes.html

<template name="recipes">
    {{#each showRecipes}}

                <div class=" col-md-4">
                    <div class="panel panel-default mb " >
                        <div class="panel-image">
                            <img src="{{this.url }}" class="panel-image-preview" />
                        </div>
                        <div class="panel-body">
                            <h4>{{name}}</h4>
                            <p>{{description}}</p>
                        </div>
                        <div class="panel-footer text-center" >
                            <p>its footer </p>
                        </div>
                    </div>
                </div>
    {{/each}}
</template>

【问题讨论】:

    标签: mongodb meteor meteor-autoform meteor-helper


    【解决方案1】:

    您是在导入cfs:ui 吗?如果没有,您应该包含它,以便您可以使用 FS.GetFile 帮助程序。然后你就可以使用这个代码了。

    食谱模板

    <template name="recipes">
        {{#each showRecipes}}
    
            <div class=" col-md-4">
                <div class="panel panel-default mb " >
                    <div class="panel-image">
    
                        {{#with FS.GetFile "recipesImages" image}}
                            <img src="{{url}}" class="panel-image-preview" />
                        {{/with}}
    
                    </div>
                    <div class="panel-body">
                        <h4>{{name}}</h4>
                        <p>{{description}}</p>
                            </div>
                            <div class="panel-footer text-center" >
                                <a href="{{pathFor 'show_recipe'}}"><span class="glyphicon glyphicon-open-file"></span></a>
                                <a href="#" ><span class="glyphicon glyphicon-time" style="vertical-align:middle"></span><small> {{time}}</small></a>
                                <a href="#"><span class="glyphicon glyphicon-heart likes" style="vertical-align:middle"></span><small>2 </small></a>
                                <a href="#"><span class="glyphicon glyphicon-share"></span></a>
                            </div>
                        </div>
                    </div>
        {{/each}}
    </template>
    

    食谱助手

    Template.recipes.helpers({
        'showRecipes':function(){
            return Recipes.find();
        }
    });
    

    这是cfs:ui documentation的链接

    【讨论】:

    • cfs:ui 没有被添加,我现在已经添加了。它仍然无法正常工作。我是初学者和自学流星,过去 4 天我一直在弄清楚如何显示图像但仍然没有成功。你可以看看我的源代码,请告诉我我现在认为的解决方案非常接近解决它。 github.com/soni1/foody@Lucas Blancas
    • @waqar,我克隆了你的 git 项目并且能够解决这个问题。请参阅上面我的更新答案。重要的变化是 {{#with FS.GetFile "recipesImages" image}}。请注意,我还删除了 recipe.images 帮助器,因为不再需要它。
    猜你喜欢
    • 1970-01-01
    • 2015-05-17
    • 2018-10-10
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    相关资源
    最近更新 更多