首页 文章

承诺拒绝不回来?

提问于
浏览
0

我正在使用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,就会发生错误并且一切都会停止 . 我've tried even putting a try/catch around it, but I can'得到任何东西回来了 . 我希望承诺拒绝并使用第二个函数以及以下响应 .

进一步潜入胆量,我想确保承诺被拒绝 . 在包中,authenticate函数被触发,它根据我在调试时放入的 console.log() 拒绝了promise . 它在拒绝中使用的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 回答

  • 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);
        });
    }
    

相关问题