【问题标题】:Handling FS.collection in meteor在流星中处理 FS.collection
【发布时间】:2015-09-01 17:04:21
【问题描述】:

我想从订阅我的图片 fs.collection 中过滤出我的结果。

集合是这样声明的:

 Images = new FS.Collection('images', {
  stores: [ImagesStore],
  filter: {
    //maxSize: 1048576 * 1, //in bytes //1MB
    maxSize: 512 * 1024, //in bytes
      allow: {
        contentTypes: ['image/*'],
        extensions: ['png', 'PNG', 'jpg', 'JPG', 'jpeg', 'JPEG', 'gif', 'GIF' ]
      },
    onInvalid: function (message) {
      if (Meteor.isClient) {
        alert("Your file must be a ['png', 'PNG', 'jpg', 'JPG', 'jpeg', 'JPEG', 'gif', 'GIF' ], and must not exceed 512KBytes");
          sAlert.error(message, {effect: 'stackslide', position: 'left-top', timeout: '3000'});
      } else {
        console.log(message);
      }
    }
  }
});

FS.HTTP.setBaseUrl('/usrImg');

Images.deny({
  insert: function(userId, fileObj) {
    return false;
  },
  update: function(userId, fileObj) {
    return false;
  },
  remove: function(userId, fileObj) {
    return false;
  },
  download: function(userId, fileObj /*, shareId*/) {
    return false;
  },
  fetch: []
});


Images.allow({
  insert: function(userId, fileObj) {
    return true;
  },
  update: function(userId, fileObj) {
    return true;
  },
  remove: function(userId, fileObj) {
    return true;
  },
  download: function(userId, fileObj /*, shareId*/) {
    return true;
  },
  fetch: []   //  ['owner']
});

订阅是这样的:

var i = -1,
array = [],
usrAvatarId = Meteor.users.find({"profile.avatar.type": "doc"}, {fields: {"profile.avatar.data": 1}}).fetch(),
nbAvatar = Meteor.users.find({"profile.avatar.type": "doc"}).count();
console.log(nbAvatar);
console.log(usrAvatarId);
while(++i < nbAvatar)
    array.push(usrAvatarId[i]._id);
console.log(array);
Meteor.subscribe('images', array);

最后,出版物是这样的:

Meteor.publish('images', function(array){
    console.log(array);
    return Images.find({});
});

目前的目标是将包含允许文件 ID 的数组作为参数传递。就像现在一样,我总是下载所有的收藏文件。

当我登录 colelction 客户端时,我确实有一个带有 _id 属性的文件数组。

但是当我尝试这个时:

Meteor.publish('images', function(array){
    console.log(array);
    return Images.find({_id: {$in: array}});
});

我没有得到任何文件,以为服务器正确记录了我传递的数组。

我可能遗漏了一些东西:我不习惯 FS.collection,它的文档不是我见过的最容易理解的...

关于允许/拒绝,我不认为它在这里改变了什么,但我没有改变它们从我过去开始的来源。

有人可以帮忙吗?每个连接下载 200 个文件越来越无聊:/

谢谢

【问题讨论】:

    标签: mongodb meteor fs


    【解决方案1】:

    基本上,您是在尝试加入以仅下载与您要发布到客户端的用户相对应的图像。

    更好、更简单、更快的方法是使用reywood:publish-composite 包来获取发布用户所在的同一出版物中的图像。

    我已经在自己的应用程序中使用 FS.collection 做了很长时间,效果非常好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-04
      • 2015-11-10
      • 2015-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多