1
votes

Currently developing an app using Ember-cli and having some dificulties with models with 2 words and relationships.

Model residents-profile

//models/residents-profile.js
DS.Model.extend({
    firstName: DS.attr('string'),
    lastName: DS.attr('string'),
    picture: DS.attr('string'),
    phone: DS.attr('string'),
    gender: DS.attr('string'),
    residentsAccount: DS.belongsTo('residentsAccount')
}

** Model Residents-account **

//models/residents-account.js
DS.Model.extend({
    email: DS.attr('string'),
    password: DS.attr('string'),
    profileId: DS.attr('number'),
    residentsProfile: DS.belongsTo('residentsProfile', this.profileId),
});

the model hook on the residents route:

//routes/residents.js

    model: function() {
            return Ember.RSVP.hash({
              residentsProfile: this.store.find('residentsProfile'),
              residentsAccount: this.store.find('residentsAccount')
            })
    }

As soon as I try and fetch just the residents profile, i get an error "residents.index Cannot read property 'typeKey'"

But if I remove the relationship key from residents-profile and call only the residents profile the data is fetched correctly.

I'm using the RESTAdapter and a Restful API,

The models are being returned separately and the response from the server is as follows:

GET /residentsProfile { "residentsProfiles": [{ "id": 20, "picture": null, "phone": null, "firstName": "Rocky", "lastName": "Balboa", "blockId": null, "unitId": null, "createdAt": "2014-09-17 19:54:28", "updatedAt": "2014-09-17 19:54:28", "residentsAccount": [5] }] }

GET /residentsAccount { "residentsAccounts": [{ "id": 5, "email": "[email protected]", "admin": false, "resident": true, "profileId": 20, "createdAt": "2014-09-17 19:54:29", "updatedAt": "2014-09-17 19:54:29", "residentsProfile": [20] }] }

EDIT

Besides the changes proposed by @Kingpin2k, which were spot on, i used setupcontroller in the following way:

setupController: function(controller, 
        controller.set('residentsAccount', models.residentsAccount);
        controller.set('residentsProfile', models.residentsProfile);
    }

Now everything works.

1

1 Answers

2
votes

Three problems, both of your relationships should be async (since they aren't returned in the same response)

residentsAccount: DS.belongsTo('residentsAccount', {async: true})

residentsProfile: DS.belongsTo('residentsProfile', {async:true})

And both of the json response from them should be a single id, not an array

 {  
   "residentsProfiles":[  
     {  
     "id":20,
     "picture":null,
     "phone":null,
     "firstName":"Rocky",
     "lastName":"Balboa",
     "blockId":null,
     "unitId":null,
     "createdAt":"2014-09-17 19:54:28",
     "updatedAt":"2014-09-17 19:54:28",
     "residentsAccount":  5
     }
   ]
}

Lastly, I'm not sure what you're trying to accomplish with this.profileId here, but it probably isn't doing what you think it is. this in that scope is probably the window, meaning you're passing in undefined likely.