【问题标题】:Promise reject not returning?承诺拒绝不返回?
【发布时间】:2015-12-25 22:47:23
【问题描述】:

我正在使用 ember.js 并且遇到了 simple auth token 包没有返回被拒绝的承诺的问题,我不知道为什么。

我要解决的问题是在身份验证被拒绝时显示错误消息,对于本示例,如果由于任何原因失败,我们甚至可以只显示硬编码消息。我看到的行为是控制台中显示了几个错误,但没有显示任何消息。

POST http://localhost:8000/v1/auth/login/ 400 (BAD REQUEST)

undefined: _emberMetalLogger["default"].error(error.stack);

// my authenticate action
authenticate: function() {
   let store = this.container.lookup('store:main');
   let credentials = this.getProperties('identification', 'password'),
      authenticator = 'simple-auth-authenticator:token';
      let authPromise = this.get('session').authenticate(authenticator, credentials);

      authPromise.then(() => {
        console.log('inside authPromise');
        let userPromise = store.find('user', {username: credentials.identification});

        userPromise.then(user => {
            console.log("inside userPromise");
            store.find('store', {user: user.get('firstObject').get('id')}).then(function(store) {
                this.get('appController').set('myStore', store.get('firstObject'));
            });
        }, err => {
            console.log('in error block');
            this.set('errorMessage', 'Unable to login with the provided credentials');
        });
    });
}

我的身份验证操作触发,但它永远无法进入错误块,也无法到达authPromise 内部。一旦它定义了 authPromise 错误就会发生并且一切都会停止。我什至尝试过尝试/捕捉它,但我无法用那个 etiher 返回任何东西。我希望承诺会拒绝并使用带有以下响应的第二个函数。

再深入一点,我想确保承诺被正确拒绝。在包中,验证功能被触发,它确实根据我在调试时输入的console.log() 拒绝了承诺。它在拒绝中使用的 2 个变量也被定义了,所以我不确定我什么时候没有收到被拒绝的承诺。

// authenticate action in the ember-simple-auth-token package
authenticate: function(credentials) {
    var _this = this;
    return new Ember.RSVP.Promise(function(resolve, reject) {
      var data = _this.getAuthenticateData(credentials);
      _this.makeRequest(data).then(function(response) {
        Ember.run(function() {
          resolve(_this.getResponseData(response));
        });
      }, function(xhr) {
        Ember.run(function() {
          console.log('rejecting');
          reject(xhr.responseJSON || xhr.responseText);
        });
      });
    });
  },

【问题讨论】:

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


    【解决方案1】:

    根据RSVP.js example,如果authPromise 拒绝,那么您可能应该在promise.catch() 而不是promise.then() 中处理它:

    authenticate() {
       let store = this.container.lookup('store:main');
       let credentials = this.getProperties('identification', 'password'),
          authenticator = 'simple-auth-authenticator:token';
    
        let authPromise = this.get('session').authenticate(authenticator, credentials);
    
        authPromise.then(() => {
          console.log('inside authPromise');
          let userPromise = store.find('user', {username: credentials.identification});
    
          userPromise.then(user => {
              console.log("inside userPromise");
              store.find('store', {user: user.get('firstObject').get('id')}).then(function(store) {
                  this.get('appController').set('myStore', store.get('firstObject'));
              });
          }).catch(err => {
              console.log('in error block');
              this.set('errorMessage', 'Unable to login with the provided credentials');
          });
        }).catch(reason => {
          console.log('Inside authPromise.catch block');
          // put your logic here, maybe reason is defined
          console.log(reason);
        });
    }
    

    【讨论】:

    • 我本来想说我已经尝试过了,但后来意识到我只是把问题放在了内部承诺上,但我实际上需要它在外部承诺上。感谢您对此以及最后一个问题的帮助!
    • 你忘了return userPromise.then…
    • 顺便说一句,在then 中处理错误并没有错。只要确保了解difference between .then(…).catch(…) and .then(…, …)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-27
    • 2016-10-16
    • 2017-12-29
    • 2021-12-20
    • 2019-11-25
    • 1970-01-01
    相关资源
    最近更新 更多