【问题标题】:Ember-data 'get' relationship promise returns null (identifier, not subdocument)Ember-data 'get' 关系承诺返回 null(标识符,而不是子文档)
【发布时间】:2016-08-18 05:31:30
【问题描述】:

我正在开发一个使用 Express/MongoDB 和 Ember 前端的应用程序。我无法访问 Ember 中的关系数据。

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

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

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

User 和 Location 模式都具有指向定义关系的组织的标识符,例如。

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

    ...

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

tl;这会返回 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() 为该 ID 找到/创建一个位置

问题是,console.log(data) 正在返回 null——而且,我无法在 Ember 检查器中查看我的模型的任何关系数据。我只看到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})
});

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

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

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

编辑:此应用程序序列化程序用于修改 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"}}}}

【问题讨论】:

  • 你使用JSONAPIAdapter吗?我真的认为您的有效载荷有问题。请显示更多回复。
  • 勒克斯,感谢您的回复。现在存在一个应用程序 JSONAPISerializer 来准备 JSON 有效负载——后端不喜欢在关系下有一个“数据”键,所以它处理这个问题。我将编辑问题以包含该代码。我意识到,当我导航到上述模型时,除了对“用户”的几次调用之外,我在 Charles 的调试代理中没有看到对后端的任何请求。这个 user.get('organization') 不应该发出请求吗?

标签: mongodb ember.js ember-data


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多