collections2和meteor-cfs-autoform上传和显示图像

时间:2015-10-27 10:24:06

标签: mongodb meteor meteor-autoform meteor-helper

我正在使用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>

1 个答案:

答案 0 :(得分:2)

您要导入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

的链接