簡體   English   中英

如何在Meteor.js中的服務器端轉換功能之后設置mongo投影?

[英]How to setup a mongo projection AFTER a server side transform function in Meteor.js?

在應用需要訪問原始文檔的轉換后,我需要限制從發布功能發送給客戶端的字段數。

我基本上是在嘗試避免向客戶端發送潛在的巨大數組,並運行一堆檢查以返回一個很好的整潔對象以供使用。

這是我現在擁有的功能-它可以正常工作,但並不是我想要的那樣,基本上限制了要觀察功能的字段。 有沒有辦法在觀察/變換之后添加投影。

Meteor.publish('network', function() {

  var self = this;

  // get the user values initially
  var user = Meteor.users.findOne(self.userId);
  var followingUsers = user.following ? user.following.users || [] : [];
  var followingChannels = user.following ? user.following.channels || [] : [];

  var transformMedia = function(doc) {
    // get the user each time to keep this publication reactive
    votesUp = doc.votes ? doc.votes.up || [] : [];
    votesDown = doc.votes ? doc.votes.down || [] : [];
    favourites = doc.votes ? doc.votes.favourites || [] : [];

    doc.userActions = {
      votedUp: _.contains(votesUp, doc._id) ? 1 : 0,
      votedDown: _.contains(votesDown, doc._id) ? 1 : 0,
      isFavourite: _.contains(favourites, doc._id) ? 1 : 0,
      played: _.contains(doc.played, self.userId) ? 1 : 0,
    };

    return doc;
  };

  var networkQuery = Media.find({
    $and: [
    {
        $and: [
          {processedAt: { $exists: true} },
          {processedStatus: 'successful'},
          {publishStatus: 'published'}
        ]
      },
      {
        // if created by this user, user they follow or channels they subscribe to
        $or: [
          {createdBy: self.userId },
          {createdBy: { $in: followingUsers} },
          {channels: { $in: followingChannels} },
        ]
      }

      // TODO : add not banned or trashed once implemented
    ]
  }, mediaModifiers).observe({
    added: function(doc) {
      self.added('media', doc._id, transformMedia(doc));
    },
    changed: function(doc, oldDoc) {
      self.changed('media', doc._id, transformMedia(doc));
    },
    removed: function(doc) {
      self.removed('media', doc._id, transformMedia(doc));
    },
  });

  self.onStop(function() {
    networkQuery.stop();
  });

  self.ready();

});

我曾經有過類似的問題 我使用cursor.observe() +一個自定義函數來處理它(就像您所做的那樣),我只是添加了一個_.pick()來過濾不必要的字段。 請看下面的發布代碼示例(尤其是白名單docToPublish部分):

var self = this;

// Modify the document we are sending to the client.
function filter(doc) {
  var length = doc.item.length;

  // White list the fields you want to publish.
  var docToPublish = _.pick(doc, [
      'someOtherField'
  ]);

  // Add your custom fields.
  docToPublish.itemLength = length;

  return docToPublish;                        
}

var handle = myCollection.find({}, {fields: {item:1, someOtherField:1}})
            // Use observe since it gives us the the old and new document when something is changing. 
            // If this becomes a performance issue then consider using observeChanges, 
            // but its usually a lot simpler to use observe in cases like this.
            .observe({
                added: function(doc) {
                    self.added("myCollection", doc._id, filter(doc));
                },
                changed: function(newDocument, oldDocument)
                    // When the item count is changing, send update to client.
                    if (newDocument.item.length !== oldDocument.item.length)
                        self.changed("myCollection", newDocument._id, filter(newDocument));
                },
                removed: function(doc) {
                    self.removed("myCollection", doc._id);                    
                });

self.ready();

self.onStop(function () {
  handle.stop();
});

這段代碼是從@datacarl答案借來的,上面提到了我的主題。

請注意,如果您最多可以擴展到多個服務器,則此方法的缺點是每個服務器都必須運行cursor.observe()函數。

您還忘記准備發布,並在發布結束時將觀察員丟棄(這可能是因為您沒有粘貼所有發布)。 它看起來像這樣:

self.ready();
self.onStop(function () {
  networkQuery.stop();
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM