Javascript 承诺拒绝不返回?
我正在使用ember.js,但是包没有返回被拒绝的承诺,我不知道为什么 我试图解决的问题是,如果身份验证被拒绝,则显示一条错误消息,例如,如果由于任何原因失败,我们甚至可以显示一条硬编码消息。我看到的行为是控制台中出现了一些错误,但没有显示任何消息Javascript 承诺拒绝不返回?,javascript,ember.js,promise,ember-simple-auth,Javascript,Ember.js,Promise,Ember Simple Auth,我正在使用ember.js,但是包没有返回被拒绝的承诺,我不知道为什么 我试图解决的问题是,如果身份验证被拒绝,则显示一条错误消息,例如,如果由于任何原因失败,我们甚至可以显示一条硬编码消息。我看到的行为是控制台中出现了一些错误,但没有显示任何消息 POSThttp://localhost:8000/v1/auth/login/ 400(错误请求) 未定义:_emberMetalLogger[“default”].error(error.stack) 我的authenticate操作启动,但它永
POSThttp://localhost:8000/v1/auth/login/ 400(错误请求)
未定义:_emberMetalLogger[“default”].error(error.stack)代码>
我的authenticate操作启动,但它永远无法进入错误块,也无法进入authPromise
内部。一旦定义了authPromise,错误就会发生,一切都会停止。我甚至试过在它周围放一个try/catch,但我无法得到任何回报。我希望承诺拒绝并使用第二个函数,并给出以下响应
再深入一点,我想确保承诺被正确拒绝。在包中启动了authenticate函数,根据调试时输入的console.log()
,它拒绝了承诺。它在拒绝中使用的两个变量也被定义了,所以我不确定何时不返回被拒绝的承诺
// 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);
});
});
});
},
根据,如果authPromise
拒绝,那么您可能应该在promise.catch()
而不是promise.then()
中处理该问题:
我本想说我已经试过了,但后来意识到我只是在内心的承诺上下了功夫,但实际上我需要在外在的承诺上下功夫。谢谢你在这个和最后一个方面的帮助!您忘记了返回userPromise。然后…
尽管如此。顺便说一句,在然后
中处理错误没有什么错。只需确保理解
// 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);
});
});
});
},
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);
});
}