承诺拒绝不返回?
Promise reject not returning?
我正在使用 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,错误就会发生,一切都会停止。我试过甚至在它周围放一个 try/catch,但我无法用那个 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);
});
});
});
},
根据 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);
});
}
我正在使用 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,错误就会发生,一切都会停止。我试过甚至在它周围放一个 try/catch,但我无法用那个 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);
});
});
});
},
根据 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);
});
}