【问题标题】:Meteor: Retrieving images within collectionsMeteor:检索集合中的图像
【发布时间】:2016-04-07 01:13:33
【问题描述】:

我正在给这个收藏写信:

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 制作的表格。

 {{> quickForm collection="GiftCards" id="giftCardsForm" type="insert"}}

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

【问题讨论】:

  • 你有没有查看 CollectionFS documentation
  • 是的,但我认为问题更多是关于 mongodb 和关系 - 我不太了解 NoSQL ......我使用 MySQL 进行连接并获取我需要的东西......
  • 那您能分享一下您编写的用于向客户端发送数据的 pub 子代码吗?也许那里出了点问题..

标签: meteor collectionfs


【解决方案1】:

您的GiftCards 集合似乎包含一个对Images 集合的外键引用,即fileId。在Images集合的主键被用作GiftCards中的外键的意义上,这实际上就像SQL一样。

现在您要显示礼品卡及其相关的单个图像。

<template name="showOneGiftCard">
  Type: {{type}}, value: {{value}} <img src={{url}}/>
</template>

那么你需要一个helper来返回图片的url:

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
  }
});

【讨论】:

  • 好的,但是如果我想使用 {{ #each 礼品卡 }} 列出所有礼品卡?
  • {{#each giftCards}}{{&gt; showOneGiftCard}}{{/each}} 然后只需创建一个返回 GiftCards.find()giftCards 助手
  • 好的,但是 let img = Images.findOne(this.fileId); // 'this' 是一张 GiftCard 的数据上下文 give "SyntaxError: Unexpected identifier 'img'" 并且没有 give: "Failed to load resource:"
  • 问题可能出在令牌和安全性方面
  • 你使用的是 Meteor 1.2 还是 1.3?如果是前者,那么 var img = 而不是 let img = 。还要确保您的图像已发布并在客户端上可用。这与令牌/安全性无关。我一直使用这种模式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-02
  • 2020-12-24
  • 2017-02-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多