Meteor Publish reactive to collection findOne

This code publishes some documents from ActiveTaskCol based on a given projection as shown.
I need to re-run this publish when ActiveTaskCol.insert(… takes place anywhere else in the project, since the value of currentAction will change.
I thought it will by reactive but I was wrong. How can this be achieved?
I tried to wrap the code in autorun function for no avail :frowning:


Meteor.publish('attributes', function () {
  var currentAction = ActiveTaskCol.findOne({}, {fields: {action:1}, sort: {createdAt: -1}, limit: 1}).action;
  return Attributes.find({action: currentAction});

Yeah that won’t work because you did a findOne, and therefore that value will never update. Use reywood:publish-composite.

Meteor.publishComposite('active', function () {
  return {
    find() {
      return ActiveTaskCol.find({}, {
        limit: 1,
        fields: { action: 1 },
        sort: { createdAt: -1 }
    children: [{
      find(active) {
        return Attributes.find({ action: active.action });
1 Like

works good. Thank you
Could you please write it in ecmaScript 5 since ecmaScript 6 is not fully supported by mobile browsers support table

you can use ecmascript package for this

Don’t worry. Meteor handles ES6 and compile it down to ES5.

How does it know when to do that, do I have control over this??

Basically, it always does.

Because ES6 is fully compatible with ES5, Meteor just compile all js codes in your project if you use package ecmascript.

1 Like