Pop-A-Stash
Pop-A-Stash

Reputation: 6652

Can't Get At Data After Find in Ember.js

I have an Ember app where the user enters in a store number. I have an Express.js API that is wired up to a MongoDB.

I setup the "store" variable in my controller like so:

App.StoreNumberRoute = Ember.Route.extend({
  setupController: function(controller, model){
      store = this.store;
  }
});

In my event catcher (i'm using Bacon.js for events) I get the number they entered and find the model using Ember syntax:

var hwStore = store.find('hwStores', { number: value });

I need to make sure that the number they entered is a valid store, and pop up a modal to show them the info about the store number they entered.

I can verify through Express.js's console messages that the correct route is being called:

GET: http://localhost:3000/hwStores?number=1234

and is in fact returning the proper result:

{ "hwStores": [
  {
    "city": "Miami",
    "country": "USA",
    "number": 1234
   }]
 }

I even have the Ember extension for chrome and I can see (by drilling down into the confusing object structure) the data returned from the call has all the details about the hardware store: city, country, etc.

The problem is, I can't get at any of the data. Nothing is working.

hwStore.objectAt(0); //<-- undefined
hwStore.firstObject; //<-- undefined
hwStore.firstObject.property('model.[]') //<-- undefined is not a function

I have no way to "get" any of the data to do my verification, and show the modal. I've tried all of these:

hwStore.objectAt(0).get('city');
hwStore.firstObject.get('city');

Nothing works. Why is this so damn confusing?

Upvotes: 0

Views: 65

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

Ember Data returns a promise, not a blocking synchronous result.

var hwStore = store.find('hwStores', { number: value });

you need to wait for the promise to resolve before you can get at the results of the call

hwStore.then(function(results){
  console.log(results.get('firstObject.city'));
  console.log(results.objectAt(0).get('city'));
});

Here's an example as well

http://emberjs.jsbin.com/OxIDiVU/506/edit

Upvotes: 2

Related Questions