0
votes

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!

1
I have an update, it seems as if the object is returned with all the correct properties, including product, but when I do a record.get('product') it returns undefined until the second call.Schnaars
Post your code, we're not wizards over here.kris
Is product a relationship like hasMany or belongsTo? It could be that you need to load it with async: false belongsTo with asyncPaul Oliver

1 Answers

0
votes

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)

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

Option #2: Wait for the promise to complete

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