0

I have a meteor app that have the following publish function (using coffeescript):

Meteor.publish "apps",  ->
  apps = Apps.find {},
    fields :
      name : 1
      description : 1
      icon : 1
  fileIds = []
  apps.forEach (doc, idx, cursor) ->
    if (doc.icon)
      fileIds.push(doc.icon)
  console.log(fileIds)
  files = Files.find {_id : { $in : fileIds}}
  [apps, files] 

Note I have a console.log in it to check when the publish function gets run. After I insert an element to collection Apps. I don't see the publish function re-run. The problem for me is that the Apps collection contains a field icon, which is the _id of an item in Files collection (using CollectionFS). when I insert an item to apps, a file is uploaded and an item is inserted to Files collection as well. However, because the publish function is not re-run, the newly created File item is not published to the client, so the client cannot see the file.

What is the problem here?

Thanks.

Fei
  • 769
  • 2
  • 7
  • 13
  • possible duplicate of [Meteor.publish: publish collection which depends on other collection](http://stackoverflow.com/questions/26398952/meteor-publish-publish-collection-which-depends-on-other-collection) – David Weldon Dec 27 '14 at 06:18

1 Answers1

0

A publish method is not re-run when there are changes to the documents available by the queries you perform in the publish method.

Your return two variables to the publish method apps & files. These two are now cursors.

When the cursor has new documents added to it, it relays the new documents down to the client. Note that this means the query remains the same, that is {_id : { $in : fileIds} will remain to the original fileIds when the method is run the first time.

There is one exception to when the publish method is re-run: when the user logs in or out.

Looking at your code you want one query to change, depending on when the other changes. For this you probably need to use another paradigm to publish such as using the publish-with-relations package : https://atmospherejs.com/cottz/publish-with-relations.

Tarang
  • 75,157
  • 39
  • 215
  • 276