Reputation: 2351
I am developing an app that uses express/MongoDB with an Ember frontend. I am failing to access the relationship data in Ember.
There are three collections: Org, User, and Location.
The Organization schema exists on it's own in mongoDB:
const organizationSchema = new Schema({
creationDate: {
type: Date,
default: Date.now
}
});
The User and Location schemas both have identifiers pointing to Organization defining the relationship, eg.
const userSchema = new Schema({
organization: {
type: Schema.ObjectId,
ref: 'Organization',
index: true,
required: true
},
...
On the frontend, In my Ember 'location' route, I am trying to get the information for the async relationship between user and organization. I want to get the organization ID with this model hook:
tl;dr this returns null
return this.store.findRecord("user", this.get("session.currentUser.id"))
.then(user => user.get('organization')).then(data => console.log("Show me the org:", data));
If I can find the org ID associated with the current user, I figure, I can find/create a location for that ID using this.store.findRecord()
Problem is, the console.log(data)
is returning null
-- and what's more, I can't view any of the relationship data for my models in the Ember inspector. I just see content: null
Is it that I am falsely representing the data in my Mongo schemas or Ember-data models? The Ember-data models:
organization.js:
export default DS.Model.extend({
location: DS.hasMany('location', {async: true}),
user: DS.belongsTo('user', {async: true})
});
user.js:
export default DS.Model.extend({
organization: DS.belongsTo('organization', {async: true}),
email: DS.attr('string'),
firstName: DS.attr('string'),
lastName: DS.attr('string'),
registrationDate: DS.attr('date'),
fullName: Ember.computed('firstName', 'lastName', function() {
return `${this.get('firstName')} ${this.get('lastName')}`;
})
});
location.js:
export default DS.Model.extend(Validations, {
organization: DS.belongsTo('organization', {async: true})
});
Currently, my requests to the GET user route on my backend return the following relationship key in their JSON payload:
{"organization":{"type":"organizations","id":"571974742ce868d575b79d6a"}}
What am I doing wrong to not be able to access this information in Ember data? Sorry for the potential over-information / general noobery. Been stuck on this for quite a while.
Edit: this Application serializer exists to modify the JSON payload relationships structure:
export default DS.JSONAPISerializer.extend({
serialize(snapshot, options) {
let json = this._super(...arguments);
// json.data.relationships.user = json.data.relationships.user.data;
json.data.relationships = _.reduce(json.data.relationships, function (rels, val, key) {
rels[key] = val.data;
return rels;
}, {});
return json;
}
});
Edit: the entire JSON payload response for findRecord('user')
{"links":{"self":"/users/5719749a2ce868d575b79d6b"},"included":[{"type":"organizations","id":"571974742ce868d575b79d6a","links":{"self":"/organizations/571974742ce868d575b79d6a"},"attributes":{"creation-date":"2016-04-22T00:46:44.779Z"}}],"jsonapi":{"version":"1.0"},"data":{"type":"users","id":"5719749a2ce868d575b79d6b","links":{"self":"/users/5719749a2ce868d575b79d6b"},"attributes":{"email":"[email protected]","first-name":"Daniel","last-name":"Thompson","registration-date":"2016-04-22T00:47:22.534Z"},"relationships":{"organization":{"type":"organizations","id":"571974742ce868d575b79d6a"}}}}
Upvotes: 0
Views: 562
Reputation: 18240
Your response already contains the organizations
record with the id 571974742ce868d575b79d6a
. So ember-data
will use that record and not fetch for a new record.
In ember all records have to be complete! This is really important to understand. You have to record with all attributes or not at all. If you need to split this you need to use two models.
So you can include all attributes in the included record, or don't sideload it at all.
Upvotes: 1