流星:收集助手还是在FS.Collection上转换? [英] Meteor: collection helpers or transform on FS.Collection?

查看:106
本文介绍了流星:收集助手还是在FS.Collection上转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用dburles:collection-helpers包,您可以在任何Mongo.collection上添加收集助手.但是我不能在FS.Collection上做到这一点.我收到 TypeError:对象[object Object]没有方法'helpers'.转换功能也不起作用.

With dburles:collection-helpers package you can add collection helpers on any Mongo.collection. But I can't do that on FS.Collection. I get TypeError: Object [object Object] has no method 'helpers'. Transform function doesn't work either.

var createUploader = function(fileObj, readStream, writeStream) {
    fileObj.uploadedBy = Meteor.users.find({_id: fileObj.uploader});
    readStream.pipe(writeStream);
};
Photos = new FS.Collection("photos", {
    stores: [
        new FS.Store.GridFS("photos", {transformWrite: createUploader})
    ],
    filter: {
        allow: {
            contentTypes: ['image/*']
        }
    }
});

不能这样做吗?请注意,当从客户端插入照片时,FS.File会得到userId,因此是fileObj.uploadedBy = Meteor.users.find({_id: fileObj.uploader});

Can't do this? Notice when a photo is inserted from the client FS.File gets userId, hence fileObj.uploadedBy = Meteor.users.find({_id: fileObj.uploader});

推荐答案

matb33-collection-helpers包通过将转换函数应用于集合来工作. CollectionFS已经应用了自己的转换功能,因此您不能使用collection helpers软件包中的转换功能覆盖它.

matb33-collection-helpers package works by applying a transform function to a collection. CollectionFS already has its own transform function applied, therefore you cannot overwrite that with ones from the collection helpers package.

根据问题跟踪器

由于CFS已经应用了转换,所以使用集合帮助器并不是一个好主意.不过,通过使用自己的函数扩展FS.File原型,您应该能够做几乎相同的事情.

Since CFS already applies a transform, it would not be a good idea to use collection helpers. You should be able to do pretty much the same thing by extending the FS.File prototype with your own functions, though.

您可以在原型上定义自定义函数.原型将可以通过this访问文档的其他属性,因此您基本上可以使用集合助手来实现相同的功能.

You could define custom functions on the prototype. The prototype will have access to other properties of the doc through this so you would basically have the same functionality with collection helpers.

另一种选择是在插入过程中将与文件相关的信息存储为单个文件对象上的元数据,例如:

Another option would be to store the file related information on the individual file object during the insert as metadata such as:

Template.photoUploadForm.events({
  'change .photoInput': function(event, template) {
    FS.Utility.eachFile(event, function(file) {
      var newPhoto = new FS.File(file);
      newPhoto.metadata = {uploadedBy: Meteor.user().profile.name};
      Photos.insert(newPhoto, function (err, fileObj) {
        if (!err) console.log(fileObj._id + " inserted!")
      });
    });
  }
});

您的代码也可以被重写以实现beforeWrite过滤器,而不是像

Your code can also be rewritten to implement a beforeWrite filter instead of a transformWrite as in

Photos = new FS.Collection("photos", {
    stores: [
        new FS.Store.GridFS("photos", {
          beforeWrite: function (fileObj) {
            fileObj.metadata = {uploadedBy: Meteor.user().profile.name};
          }
        })
    ],
    filter: {
        allow: {
            contentTypes: ['image/*']
        }
    }
});

最后,您可以选择存储用户ID并发布反应式联接

Finally, you can opt to storing the ID of the user and publishing a reactive join

Photos = new FS.Collection("photos", {
    stores: [
        new FS.Store.GridFS("photos", {
          beforeWrite: function (fileObj) {
            fileObj.metadata = {
              uploadedBy: Meteor.userId()
            };
          }
        })
    ],
    filter: {
        allow: {
            contentTypes: ['image/*']
        }
    }
});

对于出版物,您可以使用reywood:publish-composite

And for the publication, you can use reywood:publish-composite

Meteor.publishComposite('photosWithUsers', function() {
  return {
    find: function() {
      return Photos.find();
    },
    children: [
      {
        find: function(photo) {
          return Meteor.users.find(photo.uploadedBy, {
            fields: {username: 1, 'profile.name': 1}
          });
        }
      }
    ]
  };
});

当然,在客户端上,您需要订阅photosWithUsers发布.

Of course on the client, you need to subscribe to the photosWithUsers publication.

现在可以在客户端中访问该信息,因为您无法在collectionFS文档上应用转换或帮助程序,因此可以创建全局模板帮助程序:

Now to access that information in the client, since you cannot apply a transform or a helper on the collectionFS documents, you can create a global template helper:

Template.registerHelper('getUsername', function(userId) {
  check(userId, String);
  var user = Meteor.users.findOne(userId);
  return user && user.profile.name + ' (' + user.username + ')';
});

现在,您可以在模板中使用该帮助程序:

Now you can use that helper in your templates:

<template name="somePhoto">
  {{#with FS.GetFile "Photos" photo}}
    <img src="{{url}}" alt="This photo has been uploaded by {{getUsername uploadedBy}}"> 
  {{/with}}
</template>

Template.somePhoto.helpers({
  photo: function() {
    return Photos.findOne();
  }
})

这篇关于流星:收集助手还是在FS.Collection上转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆