[Beginner] Relationship not working?


#1

Hi,

I write to ask you why can’t I get my relational datas in my object.

I explain :

I created two model objects : a user and an institution. The relationship between both is Many To Many.

On the server (actually an embedded stub on /server before switching to a rails server currently in development), the relationship is the same. My server sends datas respecting the JSON API convention. By the way, the Adapter I use in the app extends JSONAPIAdapter.

All datas are correctly filled in : I can fill my user model properly when I call datas through the “find” method from the store in the service, everything’s ok :

this.get('store').find('user', id);

The user is well returned and I can check out my datas in the app.

But the user also owns an array of “institutions” and this one looks empty. I can’t parse it, and user.institutions.length returns “0”. However, when I check out the Ember Inspector, I can see the institution in “datas”, so the request seems to be functional…

I set async : true on the model.

Here are extracts of code :

  • Model user -->

      export default DS.Model.extend({
           name: DS.attr('string'),
           surname: DS.attr('string'),
           email: DS.attr('string'),
           photo: DS.attr('string'),
           institutions: DS.hasMany('institution', {async: true}),
           fullName: Ember.computed("surname", "name", function fullName(){
             return this.get('surname') + " " + this.get('name');
           }),
           avatar: Ember.computed("photo", function avatar(){
             return "/images/avatars/" + this.get('photo');
           })
      });
    

Institutions :

export default DS.Model.extend({
  "shortname": DS.attr('string'),
  "photo": DS.attr('string'),
  "address": DS.attr('string'),
  "zipcode": DS.attr('string'),
  "city": DS.attr('string'),
  "type": DS.attr('string'),
  "dataset": DS.attr('string'),
  "website": DS.attr('string'),
  "phone" : DS.attr('string'),
  "rectorat": DS.attr('string'),
  "users": DS.hasMany('user', {async: true}),
  picture: Ember.computed("photo", function picture(){
    return "images/institutions/" + this.get('photo');
  })
});

And the JSON API returned by the server :

{
   "links": {
      "self": "users/"
   },
   "data": {
      "type": "user",
      "id": 1,
      "attributes": {
         "user_id": 1,
         "name": "User",
         "surname": "Beta",
         "email": "test@unowhy.com",
         "photo": "user.jpg",
         "institutions": [
            2
         ]
      },
      "links": {
         "self": "users//1"
      },
      "relationships": {
         "institutions": [
            {
               "links": {
                  "self": "institutions/2"
               },
               "data": {
                  "type": "institutions",
                  "id": 2,
                  "attributes": {
                     "institution_id": 2,
                     "shortname": "Standford Maternelle",
                     "photo": "mrclpgnl.jpeg",
                     "address": "12 rue Riquet",
                     "zipcode": "75019",
                     "city": "Paris",
                     "type": "Lycée",
                     "dataset": "xjdfhdjhkfshfdsjkhfdskjhf",
                     "website": "http://standford.com",
                     "phone": "0478585527",
                     "rectorat": "666881548451354564",
                     "users": [
                        1
                     ]
                  }
               }
            }
         ]
      }
   },
   "included": [
      {
         "type": "institutions",
         "id": 2,
         "attributes": {
            "institution_id": 2,
            "shortname": "Standford Maternelle",
            "photo": "mrclpgnl.jpeg",
            "address": "12 rue Riquet",
            "zipcode": "75019",
            "city": "Paris",
            "type": "Lycée",
            "dataset": "xjdfhdjhkfshfdsjkhfdskjhf",
            "website": "http://standford.com",
            "phone": "0478585527",
            "rectorat": "666881548451354564",
            "users": [
               1
            ]
         },
         "links": {
            "self": "institutions/2"
         }
      }
   ]
}

Do you have an idea about why don’t I have a relationship properly and what do I have to do to have something like this :

{{#each user.institutions as |institution|}}
   {{institution.shortname}}
{{/each}} 

Thanks a lot !! :slightly_smiling: