perak / meteor-joins

Collection joins for Meteor
77 stars 7 forks source link

Error generating query with multiple joins #16

Open enzolutions opened 6 years ago

enzolutions commented 6 years ago

I am trying to do a query with 2 and more joins, the problem I found is I when I load my router directly works, but when I jump from a router menu doesn't work.

I don't know if is an issue with react-route 2.x or react-router-dom 4.x i tried with both with same behavior.

Here the code I am trying

Actions.join(Customers, "customerId", "customer", ["_id", "firstname","lastname"]);
Actions.join(ActionTypes, "actionTypeId", "type", ["name"]);

if (Meteor.isServer) {
    // This code only runs on the server
    Meteor.publish('actions', function actionsPublication() {
        // Get current customers
        var cursor = Actions.find({});
        return Actions.publishJoinedCursors(cursor);
    });
}

Maybe onely one join is possible?

enzolutions commented 6 years ago

Looks like is all about timing

My Solution (ugly but works)

I removed two joins and leave only one join and after call the subscribe I traverse the results to attach the extra info, but validating that the subscriptions are ready.

  Meteor.subscribe('actions');
  const logsSubscribe = Meteor.subscribe('logs');
  const actionsTypesSubscribe = Meteor.subscribe('actionTypes');

  const data = [];

  let actions = Actions.find().fetch();

  if(logsSubscribe.ready() && actionsTypesSubscribe.ready()) {
      _.each(actions, function (action, name) {
          var log = Logs.findOne({_id: action.logId});
          var type = ActionTypes.findOne({_id: action.actionTypeId});
          data.push({
              key: action._id,
              date: action.actionDate,
              time: action.actionTime,
              customer: action.customer.firstname + ' ' + action.customer.lastname,
              customerId: action.customer._id,
              status: action.status,
              type: type.name,
              notes: log.notes,
              files: log.files

          });
      });
  }

Maybe you could document and include a new feature that warranty that the subscriptions are ready.

leite08 commented 6 years ago

Hi @enzolutions! I've used a code very similar to yours...

Actions.join(Customers, "customerId", "customer", ["_id", "firstname","lastname"]);
Actions.join(ActionTypes, "actionTypeId", "type", ["name"]);

.. and it works for me.

Have you tried it with the latest version?

enzolutions commented 6 years ago

Not really, but I switched to Mongo Aggregations