0

I have a coffee ordering application and am trying to connect a user to their store.

I'm authenticating the user and then setting that user on the session which is based on ember-simple-auth. That part works fine setting a session.currentUser property, but then I want to set a session.myStore object as well, but I'm running into issues.

Right now I'm doing all this in an initializer:

// initializers/custom-user.js
export default {
  name: 'current-user',
  before: 'simple-auth',

  initialize: function(container, application) {
    Session.reopen({
      setCurrentUser: function() {
        let appController = container.lookup("controller:application");

        application.deferReadiness();

        if(this.get('isAuthenticated')) {
          let store = container.lookup('store:main');
          let _this = this;

          return store.find('user', 'me').then((user) => {
            // set the current user to be used on the session object
            this.set('currentUser', user);
          }).then(function(){
            // set the store for the current user
            store.find('store', {'user': _this.get('currentUser.id')}).then((data) => {
              console.log(data);
              _this.set('myStore', data);
              application.advanceReadiness();
            });
          })
        }
      }.observes('isAuthenticated')
    });
  }
};

I am getting data back where I have console.log(data); but I don't think it's the correct object. It's returning the class and not the object. In other routes/controllers I want to be able to return something like this.get('session.myStore.id') but the session.myStore does not have the data that I'm looking for

awwester
  • 9,623
  • 13
  • 45
  • 72

1 Answers1

1

According to documentation, "The return value of then is itself a promise. This second, 'downstream' promise is resolved with the return value of the first promise's fulfillment or rejection handler". Then, theoretically it is possible that first promise was rejected, user was not obtained and so _this.get('currentUser.id') is undefined. I suggest you to check this, using console.log in a handler of first then call.

Gennady Dogaev
  • 5,902
  • 1
  • 15
  • 23