meteor-reactive-publish icon indicating copy to clipboard operation
meteor-reactive-publish copied to clipboard

Issue with using observeChanges in autorun function

Open kerf007 opened this issue 6 years ago • 4 comments

Hello, Before using meteor-reactive-publish, my publish function code looked like:

let handle = cursor.observe({
        added: doc => {
          this.added('placeholders', doc._id, transform(doc))
        },
        changed: (newDoc, oldDoc) => {
          this.changed('placeholders', oldDoc._id, transform(newDoc))
        },
        removed: doc => {
          this.removed('placeholders', doc._id)
        }
})

So some transformation function was used. Then I added meteor-reactive-publish and wrapped this code in this.autorun. And now when I get DDP messages - first I get added message with some extra field from transform function, but then I get changed message that has cleared field with my extra field names and this fields are removing. Снимок экрана 2019-10-03 в 12 08 18

I can't understand why this happens and how to transform data before publish to client? Help please :)

kerf007 avatar Oct 03 '19 09:10 kerf007

I think something else is happening. See this test which seems to work.

mitar avatar Oct 03 '19 16:10 mitar

Thanks for your feedback! After that I install meteor-publish-composite package and did the task like this:

Meteor.publishComposite('placeholders', function() {
  const self = this
  return {
    find() {
      return Meteor.users.find({ _id: this.userId })
    },
    children: [
      {
        find(user) {
          const transform = doc => {
           // some transform
            return doc
          }
          let cursor = Placeholders.find(someQuery)

          let handle = cursor.observe({
            added: doc => {
              self.added('placeholders', doc._id, transform(doc))
            },
            changed: (newDoc, oldDoc) => {
              self.changed('placeholders', oldDoc._id, transform(newDoc))
            },
            removed: oldDoc => {
              self.removed('placeholders', oldDoc._id)
            }
          })

          self.onStop(() => handle.stop())
          return cursor
        }
      }
    ]
  }
})

And I get the same problem - first added message and than changed with cleared field. After that I removed meteor-reactive-publish and it's working now... But I can't figure out what was it. May be it some package incompatibility or something else..

kerf007 avatar Oct 03 '19 20:10 kerf007

If you can provide a small working reproduction (with the least number of packages, just those really required for reproduction), then there is a chance to help here. Otherwise not really.

mitar avatar Oct 03 '19 20:10 mitar

I created test project: https://github.com/kerf007/meteor-publish-test The issue is reproducing or I don't understand something..

kerf007 avatar Oct 03 '19 21:10 kerf007