Ember数据未加载我的所有数据

时间:2022-03-24 23:01:36

I am having troubles with ember data and I was seeking some help. I am using findRecord to return a single record('location') and then getting a single attribute out of that record('product'). There are other attributes that return just fine(phone, name, etc), however "product" does not return until I have fired the model call at least twice. I have checked my network tab and it is coming in fine from the api, but it does not seem to be loading into ember data(until it fires twice). Has anyone else come across this? I am completely stumped. Thanks!

我在使用余烬数据时遇到麻烦,我正在寻求帮助。我使用findRecord返回单个记录('location'),然后从该记录中获取单个属性('product')。还有其他属性返回正常(电话,名称等),但是“产品”在我至少两次触发模型调用之前不会返回。我检查了我的网络标签,它从api进入正常,但它似乎没有加载到ember数据(直到它激发两次)。还有其他人遇到过这个吗?我完全难过了。谢谢!

1 个解决方案

#1


0  

It looks to me that you have a model that is defined as follows:

在我看来,你有一个定义如下的模型:

/* location model */
export default Model.extend({
  phone: attr(),
  name: attr(),
  /*...*/
  product: belongsTo('product')
});

Then the code you are trying to execute is most probably something like:

那么你试图执行的代码很可能是这样的:

let name = location.get('name'); // This returns immediately.
let product = location.get('product'); // This does not work as expected

If that is the case then your problem is that you are trying to acquire the product from the location synchronously while it is an asynchronous relationship. This means that you have two options:

如果是这种情况,那么您的问题是,当它是异步关系时,您试图从该位置同步获取该产品。这意味着您有两种选择:

Option #1: Make the relationship synchronous (as mentioned by Paul Oliver)

选项#1:使关系保持同步(如Paul Oliver所述)

/* location model */
export default Model.extend({
  phone: attr(),
  name: attr(),
  /*...*/
  product: belongsTo('product', {async: false})
});

Option #2: Wait for the promise to complete

选项#2:等待承诺完成

location.get('product').then(function(product) {
  // Do something with product here
});

#1


0  

It looks to me that you have a model that is defined as follows:

在我看来,你有一个定义如下的模型:

/* location model */
export default Model.extend({
  phone: attr(),
  name: attr(),
  /*...*/
  product: belongsTo('product')
});

Then the code you are trying to execute is most probably something like:

那么你试图执行的代码很可能是这样的:

let name = location.get('name'); // This returns immediately.
let product = location.get('product'); // This does not work as expected

If that is the case then your problem is that you are trying to acquire the product from the location synchronously while it is an asynchronous relationship. This means that you have two options:

如果是这种情况,那么您的问题是,当它是异步关系时,您试图从该位置同步获取该产品。这意味着您有两种选择:

Option #1: Make the relationship synchronous (as mentioned by Paul Oliver)

选项#1:使关系保持同步(如Paul Oliver所述)

/* location model */
export default Model.extend({
  phone: attr(),
  name: attr(),
  /*...*/
  product: belongsTo('product', {async: false})
});

Option #2: Wait for the promise to complete

选项#2:等待承诺完成

location.get('product').then(function(product) {
  // Do something with product here
});