承诺拒绝不回来?

时间:2015-09-28 16:05:55

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

我正在使用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,就会发生错误并且一切都会停止。我甚至试过尝试/捕捉它,但我无法得到任何回报。我希望承诺拒绝并使用第二个函数以及以下响应。

进一步潜入胆量,我想确保承诺被拒绝了。在包中,authenticate函数被触发,它根据我在调试时输入的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);
        });
      });
    });
  },

1 个答案:

答案 0 :(得分:2)

根据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);
    });
}
相关问题