How to publish a view/transform of a collection in Meteor?

前端 未结 3 484
没有蜡笔的小新
没有蜡笔的小新 2020-12-03 02:39

I have made a collection

var Words = new Meteor.Collection(\"words\");

and published it:

Meteor.publish(\"words\", functio         


        
3条回答
  •  春和景丽
    2020-12-03 03:02

    UPDATE You can transform a collection on the server like this:

    Words = new Mongo.Collection("collection_name"); 
    
    Meteor.publish("yourRecordSet", function() {
    
      //Transform function
      var transform = function(doc) {
        doc.date = new Date();
        return doc;
      }
    
      var self = this;
    
      var observer = Words.find().observe({
          added: function (document) {
          self.added('collection_name', document._id, transform(document));
        },
        changed: function (newDocument, oldDocument) {
          self.changed('collection_name', oldDocument._id, transform(newDocument));
        },
        removed: function (oldDocument) {
          self.removed('collection_name', oldDocument._id);
        }
      });
    
      self.onStop(function () {
        observer.stop();
      });
    
      self.ready();
    
    });
    

提交回复
热议问题