我试图在发布中发布一个isFriend返回记录的自定义计算字段Meteor.user.
我尝试了各种解决方案,但没有一个看到工作:
从出版物转换不起作用,如此处所述... /sf/answers/1284121821/
我在这里尝试了解决方案如何添加temp.字段到Meteor发布 ..买不起作用...请参阅下面的代码
码:
Meteor.publish("viewProfile", function(id) {
console.log("Finding the user: " + id);
var self = this;
Meteor.users
.find({"_id": id})
.forEach(function(entry) {
entry.isFriend = true; // this function changes the content of entry
self.added("Meteor.users", entry._id, entry);
});
self.ready();
});
Run Code Online (Sandbox Code Playgroud)
请指教.
转换文档的最简单方法是为集合添加一个transform选项.您可以使用meteor API直接执行此操作,也可以使用像collection-helpers这样的包(请参阅文档以获取更多详细信息).
但是,有时您需要在文档发布之前对其进行转换,因为只有服务器具有必要的信息.一个很好的例子是签名的URL.在这种情况下,您可以使用observe或observeChanges操作每个对象.
observeChanges效率更高但它只能在部分文档上运行(例如,如果您想要转换已经存在的单个字段).在您的示例中,您需要查看整个文档才能添加字段,因此您需要一个observe.尝试以下内容:
Meteor.publish('viewProfile', function(userId) {
check(userId, String);
// modify this as needed
var transform = function(user) {
user.isFriend = true;
return user;
};
// only publish the fields you really need
var fields = {username: 1, emails: 1, profile: 1};
var self = this;
var handle = Meteor.users.find(userId, {fields: fields}).observe({
added: function (user) {
self.added('users', user._id, transform(user));
},
changed: function (user) {
self.changed('users', user._id, transform(user));
},
removed: function (user) {
self.removed('users', user._id);
}
});
this.ready();
this.onStop(function() {
handle.stop();
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1498 次 |
| 最近记录: |