dimanche 24 avril 2016

Ember-data 'get' relationship promise returns null (identifier, not subdocument)

I am working with express/MongoDB and having trouble getting Ember-data to work with the relationships between these 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 to define the relationship.

const userSchema = new Schema({
    organization: {
        type: Schema.ObjectId,
        ref: 'Organization',
        index: true,
        required: true
    },

    ...

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:

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 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.

Thanks so much for any help! <3 <3




Aucun commentaire:

Enregistrer un commentaire