2016-04-05 51 views
0

我寫這個集合中檢索圖片:流星:收藏

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

與自動窗體做了一個表格。

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

數據寫入,但我沒有找到一個方法來顯示與集合中的每個文檔相關的圖像......我只有ID(列在FileID),並沒有找到發佈正確的圖像與它指的特定文檔的方式...

+0

你有沒有檢查出CollectionFS [文件](https://github.com/CollectionFS/Meteor-CollectionFS#顯示的上傳的圖像)? –

+0

是的,但我認爲問題更多關於mongodb和關係 - 我不太瞭解NoSQL .....與MySQL我做了一個連接,並採取我需要的...... –

+0

然後你可以分享pub sub您編寫的代碼將數據發送給客戶端?也許有什麼地方是錯誤的.. –

回答

0

它看起來像你的GiftCards集合包括一個單一的外鍵參考Images集合fileId。這實際上就像SQL,因爲Images集合的主鍵在GiftCards中用作外鍵。

現在您想要顯示禮品卡及其相關的單張圖片。

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

然後,你需要一個幫手返回圖像的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 
    } 
}); 
+0

好的,但如果我想要使用{{#each禮品卡}}的所有禮品卡列表? # –

+0

'{{#每個giftCards}} {{/每個}}'然後只是做一個'giftCards'幫手,返回'GiftCards.find()' –

+0

好的完成,但與讓img =圖像。 findOne(this.fileId); //'this'是一個GiftCard的數據上下文給「SyntaxError:意外標識符'img'」,並且沒有給出:「未能加載資源:」 –