mongodb - Ember-data 'get' relationship promise returns null (identifier, not subdocument) -
i developing app uses express/mongodb ember frontend. failing access relationship data in ember.
there 3 collections: org, user, , location.
the organization schema exists on it's own in mongodb:
const organizationschema = new schema({ creationdate: { type: date, default: date.now } });
the user , location schemas both have identifiers pointing organization defining relationship, eg.
const userschema = new schema({ organization: { type: schema.objectid, ref: 'organization', index: true, required: true }, ...
on frontend, in ember 'location' route, trying information async relationship between user , organization. want organization id model hook:
tl;dr returns null
return this.store.findrecord("user", this.get("session.currentuser.id")) .then(user => user.get('organization')).then(data => console.log("show me org:", data));
if can find org id associated current user, figure, can find/create location id using this.store.findrecord()
problem is, console.log(data)
returning null
-- , what's more, can't view of relationship data models in ember inspector. see content: null
is falsely representing data in mongo schemas or ember-data models? 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, requests user route on backend return following relationship key in json payload:
{"organization":{"type":"organizations","id":"571974742ce868d575b79d6a"}}
what doing wrong not able access information in ember data? sorry potential over-information / general noobery. been stuck on quite while.
edit: application serializer exists modify 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: entire json payload response 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":"danthwa@gmail.com","first-name":"daniel","last-name":"thompson","registration-date":"2016-04-22t00:47:22.534z"},"relationships":{"organization":{"type":"organizations","id":"571974742ce868d575b79d6a"}}}}
your response contains organizations
record id 571974742ce868d575b79d6a
. ember-data
use record , not fetch new record.
in ember records have complete! important understand. have record attributes or not @ all. if need split need use 2 models.
so can include attributes in included record, or don't sideload @ all.
Comments
Post a Comment