如何在CollectionFS中将图像从自动表单链接到用户集合以显示个人资料图片

How to link image from autoform to user collection in CollectionFS for displaying a profile picture?

本文关键字:集合 用户 链接 显示 资料图片 表单 CollectionFS 图像      更新时间:2023-09-26

使用autoform保存用户上传到"images"集合中的个人资料图片。然而,我不知道如何调用特定的图片被加载到模板上。以下命令不显示模板上的图像。但是,如果我将Images.find({'doc.metadata.ownerId':Meteor.userId()});更改为Images.find(),则会加载所有图像,但我不能指定我想要的是哪一个。

this.Images = new FS.Collection("Images", {
  stores: [new FS.Store.GridFS("Images", {})]
});
Images.files.before.insert(function(doc) {
  doc.metadata = {
    date: Date.now(),
    ownerId: this.userId
             };
  console.log("before", doc);
  return doc;
});
Template.photos.helpers({
      Images: function () {
        Images.find({'doc.metadata.ownerId':Meteor.userId()});
                }
     });

您正在做的是用户上传的图像,您正在添加userid作为元值并存储它。它只允许你获取用户上传的所有图片。由于您还使用了时间戳,所以最新的一个将是最新的个人资料图片。

或者在用户文档中,在"profile"后面增加"photo"字段。

这样,当用户上传照片时,将photo值设置为所上传图像的键,并且为了显示照片,您可以执行

Images.findOne({ _id: Meteor.user().profile.photo })

如果您使用autoform,在profile.photo中存储图像ID将会容易得多。

这个怎么样:

Template.photos.helpers({
  Images: function () {
    Images.find({'metadata.ownerId':Meteor.userId()});
  }
});