【问题标题】:ember set user object based on authenticated userember 基于认证用户设置用户对象
【发布时间】:2015-09-18 15:45:27
【问题描述】:

我有一个咖啡订购应用程序,正在尝试将用户连接到他们的商店。

我正在对用户进行身份验证,然后在基于 ember-simple-auth 的会话中设置该用户。该部分可以很好地设置 session.currentUser 属性,但我也想设置 session.myStore 对象,但我遇到了问题。

现在我在初始化器中做这一切:

// 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')
    });
  }
};

我正在将数据返回到我拥有console.log(data); 的位置,但我认为这不是正确的对象。它返回的是类而不是对象。在其他路线/控制器中,我希望能够返回类似 this.get('session.myStore.id') 的内容,但 session.myStore 没有我正在寻找的数据

【问题讨论】:

    标签: javascript ember.js ember-simple-auth


    【解决方案1】:

    根据documentation,“then 的返回值本身就是一个promise。第二个,'下游'promise 是用第一个promise 的实现或拒绝处理程序的返回值解决的”。然后,理论上有可能第一个 promise 被拒绝,用户没有被获取,所以 _this.get('currentUser.id') 是未定义的。我建议您在第一个 then 调用的处理程序中使用 console.log 来检查这一点。

    【讨论】:

      猜你喜欢
      • 2014-05-27
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 2017-11-14
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多