根据ID显示头像

Display profile picture based on ID

  1. 用户注册,所有数据都在集合中"users"(流星用户帐户)。
  2. 在个人资料页面中,用户可以上传个人资料图片(流星文件)。图片元数据在集合 'images'.
  3. 中注册

每个人都可以浏览个人资料,它工作得很好,除了我需要动态更改每张显示的卡片上的个人资料图片的 src link。当我需要的信息在另一个集合中时,我不确定如何处理。

图片集:

{
    "_id" : "LT6aCTFkbRn6AAKBg",
    "size" : 1467914,
    "type" : "image/jpeg",
    "name" : "profil.jpg",
    "meta" : {},
    "ext" : "jpg",
    "extension" : "jpg",
    "extensionWithDot" : ".jpg",
    "mime" : "image/jpeg",
    "mime-type" : "image/jpeg",
    "userId" : "3JAt887HkSooRRPk2",
    "path" : "assets\app\uploads\images\LT6aCTFkbRn6AAKBg.jpg",
    "versions" : {
        "original" : {
            "path" : "assets\app\uploads\images\LT6aCTFkbRn6AAKBg.jpg",
            "size" : 1467914,
            "type" : "image/jpeg",
            "extension" : "jpg"
        }
    },
    "_downloadRoute" : "/cdn/storage",
    "_collectionName" : "images",
    "isVideo" : false,
    "isAudio" : false,
    "isImage" : true,
    "isText" : false,
    "isJSON" : false,
    "isPDF" : false,
    "_storagePath" : "assets\app\uploads\images",
    "public" : false
}

_id (LT6aCTFkbRn6AAKBg) 不是用户 ID。

Template.profileDetails.helpers({
   user: function() {
    return Meteor.users.find();
  },

  PSEUDO:
  imageLinkForThisUser: function() {
    return Images.find('document in collection which has same userId key in it and return path');
  },

谢谢!

如果用户已登录,

Meteor.userId() 将 return 为当前 _id 用户。

所以您的 mongo 查询应该是:

Images.findOne({userId: Meteor.userId()})['path'];

您可以使用 findOne 而不是 find,因为(我假设)每个用户只能存储一张图像。

您的 helper 函数将是:

Template.profileDetails.helpers({
   ...
   imageLinkForThisUser: function() {
    return Images.findOne({userId: Meteor.userId()})['path'];
  },
  ...
});

HTML:

<img src={{imageLinkForThisUser}} />

旁白:您不需要 return 用户标识/用户数据的助手。可以直接用handlebars取回。

Eg. {{currentUser.username}}

this 回答

中有更多相关信息