Replace attributes of a document in publish function

后端 未结 2 2061
清酒与你
清酒与你 2021-01-16 02:17

I\'m using meteor and I have a question about the publish function (server side)

Meteor.publish(\'users\', function () { .... }

I\'m sendin

相关标签:
2条回答
  • 2021-01-16 02:38

    You could do this:

    Meteor.publish("tasks", function() {
    
        var transform = function(task) {
            var project = Projects.findOne({_id: task.projectId});
            task.projectTitle = project.title;
            return task;
        }
    
        var self = this;
    
        var tasks = Tasks.find().observe({
            added: function (document) {
                self.added('tasks', document._id, transform(document));
            },
            changed: function (newDocument, oldDocument) {
                self.changed('tasks', document._id, transform(newDocument));
            },
            removed: function (oldDocument) {
                self.removed('tasks', oldDocument._id);
            }
        });
    
        self.ready();
    
        self.onStop(function () {
            tasks.stop();
        });
    
    });
    

    There's a lot of custom logic there, but the 'transform' basically adds the attributes in.

    0 讨论(0)
  • 2021-01-16 02:44

    Your code looks good but you're forgetting the .fetch() method on your task request. It should be var tasks = Tasks.find().fetch();

    0 讨论(0)
提交回复
热议问题