Sivailango
Sivailango

Reputation: 564

Meteor : Relationship with Join

I have two collections Client, WorkClassification. Client object can have more than one WorkClassification. So my document structure is

WorkClassification:

{ "classification" : "Web search", "isActive" : true, "_id" : "Htdv9RKrrCbYwPpEt" }
{ "classification" : "Web Tool", "isActive" : true, "_id" : "DS8QAaFnk5XHbcFhH" }
{ "classification" : "Web Work", "isActive" : true, "_id" : "P6kebK7t3BekuHaBP" }

Client:

{ "name" : "Test", "code" : "test", 
   "classifications" : [ 
               { "id" : "DS8QAaFnk5XHbcFhH" }, 
               { "id" : "P6kebK7t3BekuHaBP" }
              ]
}

Now i want to display the client document with classificaitons details by each classification id.

Helper:

Template.viewClient.helpers({
    client : function() {
        return Client.findOne(Session.get('clientId')); // I have set this value on Router, i can get client values
    },
    classifications: function() {
        var client = Client.findOne(Session.get('clientId'));
        console.log(client.classifications); // I can get Array object here
        return WorkClassification.find({_id : {$in: client.classifications }}); // It does not work
    }
})

Log:

"Exception in template helper: .classifications@http://test:3000/client/views/client/client.js?b6fa170a4dab9684d087c7186827b0c9af0d37aa:185:6
bindDataContext/<@http://localhost:3000/packages/blaze.js?efa68f65e67544b5a05509804bf97e2c91ce75eb:2727:14
Blaze._wrapCatchingExceptions/<@http://localhost:3000/packages/blaze.js?efa68f65e67544b5a05509804bf97e2c91ce75eb:1606:14
Spacebars.call@http://localhost:3000/packages/spacebars.js?3c496d2950151d744a8574297b46d2763a123bdf:169:12
Template.viewClient</<@http://localhost:3000/client/views/client/template.viewClient.js?33c6b442d3e5543e6d54fbf7cfef6f1a55e69a3c:8:12
Blaze.Each/</<@http://localhost:3000/packages/blaze.js?efa68f65e67544b5a05509804bf97e2c91ce75eb:2521:27
viewAutorun/<@http://localhost:3000/packages/blaze.js?efa68f65e67544b5a05509804bf97e2c91ce75eb:1808:14
Blaze._withCurrentView@http://localhost:3000/packages/blaze.js?efa68f65e67544b5a05509804bf97e2c91ce75eb:2043:12
viewAutorun@http://localhost:3000/packages/blaze.js?efa68f65e67544"[…] meteor.js:887
Array [ Object, Object ] client.js:185

I am struggling on this for one week.

Upvotes: 0

Views: 193

Answers (2)

David Weldon
David Weldon

Reputation: 64312

You're close, but you need to extract all of the id values rather than {id: value} pairs. This should work:

Template.viewClient.helpers({
  classifications: function() {
    var client = Client.findOne(Session.get('clientId'));

    if(client && client.classifications) {
      var cids = _.pluck(client.classifications, 'id');
      return WorkClassification.find({_id: {$in: cids}});
    }
  }
});

That should fix your immediate problem, but I think you are storing your ids in an overly complex way (unless there are other details you are not showing). I'd recommend structuring your clients like this:

{
  name: 'Test',
  code : 'test',
  classifications: ['DS8QAaFnk5XHbcFhH', 'P6kebK7t3BekuHaBP'}]
}

Then your original code will work, and you can just simple operators like $addToSet and $pull to modify client.classifications.

Upvotes: 1

Sivailango
Sivailango

Reputation: 564

Above solution was worked. Now i kept classifications id as your suggestion. It works. But i could see below exception on console.

Exception in template helper: .classifications@http://localhost:3000/client/views/order/order.js?e222316ea2c76d074abe5daef282e5dda46b77d7:56:45
bindDataContext/<@http://localhost:3000/packages/blaze.js?efa68f65e67544b5a05509804bf97e2c91ce75eb:2727:14
Blaze._wrapCatchingExceptions/<@http://localhost:3000/packages/blaze.js?efa68f65e67544b5a05509804bf97e2c91ce75eb:1606:14
Spacebars.call@http://localhost:3000/packages/spacebars.js?3c496d2950151d744a8574297b46d2763a123bdf:169:12
Template.createOrder</<@http://localhost:3000/client/views/order/template.createOrder.js?7dcce1ded5c361bac6f1aafdd5fe90f410d64000:162:12
Blaze.Each/</<@http://localhost:3000/packages/blaze.js?efa68f65e67544b5a05509804bf97e2c91ce75eb:2521:27
viewAutorun/<@http://localhost:3000/packages/blaze.js?efa68f65e67544b5a05509804bf97e2c91ce75eb:1808:14
Blaze._withCurrentView@http://localhost:3000/packages/blaze.js?efa68f65e67544b5a05509804bf97e2c91ce75eb:2043:12
viewAutorun@http://localhost:3000/packages/blaze.js?efa68f65e6754"[…]

Upvotes: 0

Related Questions