Ember-data 'get' 关系承诺 returns null(标识符,不是子文档)

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

我正在开发一个使用 express/MongoDB 和 Ember 前端的应用程序。我无法访问 Ember.

中的关系数据

共有三个集合:Org、User 和 Location。

组织架构在 mongoDB 中独立存在:

const organizationSchema = new Schema({
    creationDate: {
        type: Date, 
        default: Date.now
    }
});

用户和位置模式都有指向定义关系的组织的标识符,例如

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

    ...

在前端,在我的 Ember 'location' 路由中,我试图获取用户和组织之间异步关系的信息。我想用这个模型钩子获取组织 ID:

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));

如果我能找到与当前用户关联的组织 ID,我想,我可以使用 this.store.findRecord()

find/create 该 ID 的位置

问题是,console.log(data) 正在 returning null -- 而且,我无法在 [=67] 中查看我的模型的任何关系数据=] 检查员。我刚看到 content: null

我是否在 Mongo 模式或 Ember 数据模型中错误地表示数据? Ember-数据模型:

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})
});

目前,我对后端 return 上的 GET 用户路由的请求在其 JSON 负载中的以下关系键:

{"organization":{"type":"organizations","id":"571974742ce868d575b79d6a"}}

无法访问 Ember 数据中的此信息,我做错了什么?对于潜在的过度信息/一般笨拙,我们深表歉意。卡在这上面好久了。

编辑:存在此应用程序序列化程序以修改 JSON 有效负载关系结构:

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;
    }
});

编辑:findRecord('user')

的整个 JSON 负载响应
{"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"}}}}

您的回复已包含 ID 为 571974742ce868d575b79d6aorganizations 记录。所以 ember-data 将使用该记录而不是获取新记录。

在ember所有记录必须完整!理解这一点非常重要。您必须记录所有属性或根本不记录。如果你需要拆分这个你需要使用两个模型。

因此您可以在包含的记录中包含所有属性,或者根本不加载它。