首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >流星:在集合中检索图像

问流星:在集合中检索图像
EN

Stack Overflow用户
提问于 2016-04-05 14:37:34
回答 1查看 39关注 0票数 0

我写信给这个收藏品:

代码语言:javascript
复制
GiftCards = new Mongo.Collection('giftcards');

GiftCards.attachSchema(new SimpleSchema({
    cardType: {
        type: String
    },
    cardValue: {
        type: Number,
        defaultValue: 100
    },
    fileId: {
        type: String,
        autoform: {
            afFieldInput: {
                type: "fileUpload",
                collection: "Images"
            }
        }
    }
}));

Images = new FS.Collection("images", {
    stores: [new FS.Store.FileSystem("images", {path: "public/uploads"})]
});

用AutoForm制作的表格。

代码语言:javascript
复制
 {{> quickForm collection="GiftCards" id="giftCardsForm" type="insert"}}

数据是写入的,但我找不到一种方法来显示与集合中的每个文档相关联的图像.我只有id (在fileId中),也没有找到正确发布它引用的特定文档的图像的方法.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-05 18:27:48

看起来,您的GiftCards集合包含了对Images集合的单个外键引用,作为fileId。这实际上与SQL类似,因为Images集合的主键在GiftCards中用作外键。

现在你要展示一张礼品卡及其相关的单张图片。

代码语言:javascript
复制
<template name="showOneGiftCard">
  Type: {{type}}, value: {{value}} <img src={{url}}/>
</template>

然后需要一个助手来返回图像的url:

代码语言:javascript
复制
Template.showOneGiftCard.helpers({
  url: function(){
    var img = Images.findOne(this.fileId); // 'this' is the data context of one GiftCard
    return img && img.url(); // will return undefined if there's no attached image
  }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36429492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档