Ember 数据相关键未定义 [英] Ember data dependent keys undefined

查看:25
本文介绍了Ember 数据相关键未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于这个主题的许多其他帖子已经有 2 年以上的历史了,所以这里有一个可能很简单的问题.

A lot of the other posts on this topic are 2+ years old, so here goes a potentially simple question.

我正在使用 Ember 数据关系让bizinfo"记录属于用户"记录.看起来很简单,但我正处于最糟糕的时期.

I am using Ember data relationships to have a 'bizinfo' record belong to a 'user' record. Seems simple, but I am having the worst time of it.

在 app/models/bizinfo.js 我有一行:

In app/models/bizinfo.js I have the line:

'ownedBy': DS.belongsTo('user')

在我的路线中,我验证并保存模型,我有以下代码:

And in my route, where I validate and then save the model, I have the following code:

user_id: Ember.computed(function(){
    return `${this.get('session.data.authenticated.user_id')}`;
  }),

  user: Ember.computed(function(){
    return this.store.findRecord('user', this.get('user_id'));
  }),

  model(params){
    return this.store.createRecord('bizinfo', {'ownedBy': this.get('user')});
  },

此时,如果我进入 Ember 检查器查看 'bizinfo' 数据对象,我会在belongsTo 选项卡下看到以下内容:

at this point if I go into the Ember inspector to look at the 'bizinfo' data object, I see the following under the belongsTo tab:

ownedBy : <(subclass of Ember.ObjectProxy):ember1053>

这是我提交操作的代码:

Here is the code from my submit action:

submit() {
  let model = this.currentModel;
  console.log(model.ownedBy);
  console.log(`what does the model look like?`);
  console.log(model.toJSON());
  model.validate().then(({model, validations}) => {
    if (validations.get('isValid')) {
      this.setProperties({
        showAlert: false,
        isRegistered: true,
        showCode: false
      });
      let success = (response) => {
        console.log(`Server responded with ${response.toJSON()}`);
      };

      let failure = (response) => {
        console.log(`Server responded with ${response}`);
      };
      model.save().then(success, failure);
    } else {
      this.set('showAlert', true);
    }
    this.set('didValidate', true);
  }, (errors) => {
    console.log(`errors from failed validation: ${errors}`);
  });
},

这里是第一个 console.log 语句的结果:

So here is the result of the first console.log statement:

ComputedProperty {isDescriptor: true, _dependentKeys: undefined, _suspended: undefined, _meta: Object, _volatile: false…}

当我查看 model.toJSON() 日志时,我看到

And when I look at the model.toJSON() log, I see

ownedBy: null

谁能看出这里出了什么问题?是创建记录语句吗?我尝试了很多不同的排列方式(例如仅提交 id 作为用户"参数.

Can anyone see what's going wrong here? Is it the create record statement? I have tried a lot of different permutations (such as submitting just the id as the 'user' parameter.

推荐答案

findRecord 将返回一个 promise.解决这个问题的一个简单方法是

findRecord will return a promise. A simple way to get around the issue is

model(params){
  return this.store.findRecord('user', this.get('user_id')) .
    then(ownedBy => this.store.createRecord('bizinfo', {ownedBy});
}

这将等待 findRecord 解析,然后返回一个具有解析值的新记录作为 ownedBy 属性.

This will wait for the findRecord to resolve, then return a new record with the resolved value as the ownedBy property.

这篇关于Ember 数据相关键未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆