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

查看:56
本文介绍了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语句的结果: p>

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提交为'user'参数。

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 将返回承诺。解决问题的一个简单方法是

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天全站免登陆