javascript - Meteor Collection Transform : is it done on the server or on the client? 或者它取决于

标签 javascript meteor

我想使用转换从集合中创建一个“虚拟字段”。但是,我添加的新字段(在转换函数中)正在向返回的文档中添加相当多的数据。

如果转换发生在客户端内部,这很好。如果在服务器端完成,则会出现带宽问题。

所以我想知道转换是在服务器上还是在客户端上完成,还是取决于我如何查找/获取文档?

最佳答案

更新:可以在服务器上进行转换。

您可以像这样在客户端进行转换:

return YourCollection.find({}, {transform: function (doc) {
   doc.test = true;
   return true;
}});

Meteor 忽略对已发布查询的transform(来自Meteor.publish)。客户端看到文档时就好像转换不存在一样。

如果您想在服务器上使用转换,您可以这样做:

YourCollection = 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 = YourCollection.find().observe({
      added: function (document) {
      self.added('collection_name', document._id, transform(document));
    },
    changed: function (newDocument, oldDocument) {
      self.changed('collection_name', newDocument._id, transform(newDocument));
    },
    removed: function (oldDocument) {
      self.removed('collection_name', oldDocument._id);
    }
  });

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

  self.ready();

});

关于javascript - Meteor Collection Transform : is it done on the server or on the client? 或者它取决于,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18093560/

相关文章:

javascript - 使用 Angular 8 将 NPM 包 Stockfish.js(国际象棋引擎)编译为 Web Worker 时出错

linux - 陨石(mrt)命令什么都不做

javascript - 在 meteor 中设置简单事件

curl SSL 认证错误

javascript - 如何使用 Meteor Cloudinary 显示另一个用户的个人资料图像项目

javascript - 无法访问获取的数组元素

javascript - 将字符串(看起来像数组)转换为多维数组

javascript - 在 AngularJS 中加密 ngModel 值

javascript - 使用 debounce 将 React 状态同步到 Meteor 集合

javascript - 随着鼠标指针位置的改变而改变文本?