Ben*_*Ben 6 promise angularfire2 angular google-cloud-firestore
在带有 FireStore 和 angularfire2 的 Angular 5 中,通过控制器从服务获取文档时处理错误的正确方法是什么?
服务:
getInviteById( inviteId: string ): Promise<any> {
// get requested invite from firestore
var inviteDocument = this.afs.collection( 'invites' ).doc( inviteId );
let invite = inviteDocument.ref.get().then( doc => {
// if document exists
if (doc.exists) {
// return id and data
const id = doc.id;
var data = doc.data() as any;
return { id, ...data };
// if document does not exist
} else {
console.log("Error: No such document!");
// WHAT DO I NEED TO RETURN HERE???
}
// if other error
}).catch(function(error) {
console.log("Error: Getting document:", error);
// WHAT DO I NEED TO RETURN HERE???
});
// return invite
return invite;
};
Run Code Online (Sandbox Code Playgroud)
控制器:
this.inviteService.getInviteById( inviteId )
.then( resolve => {
this.invite = resolve;
})
.catch( err => {
// THIS NEVER GETS CALLED !
console.log("INVITE-COMPONENT: Cannot get invite for this id." );
});
Run Code Online (Sandbox Code Playgroud)
如果 FireStore 中存在具有邀请 ID 的文档,则一切正常。但是,如果 FireStore 中没有与邀请 ID 对应的文档,则该服务将记录“错误:没有此类文档!” (正如预期的那样),但是组件不会进入它自己的catch案例。
如何处理我的组件中的“无此类文档”错误,以便我可以相应地修改我的 UI?
您可以返回被拒绝的承诺,但更简单throw。
因此,直接地,你可以写:
// (1) ILLUSTRATIVE - NOT YET THE FULL SOLUTION
getInviteById(inviteId: string): Promise<any> {
var inviteDocument = this.afs.collection('invites').doc(inviteId);
return inviteDocument.ref.get()
.then(doc => {
if (doc.exists) { // if document exists ...
const id = doc.id;
var data = doc.data() as any;
return {id, ...data}; // ... return id and data.
} else { // if document does not exist ...
throw new Error('No such document!'); // ... throw an Error.
}
})
.catch(error => {
throw new Error('Error: Getting document:'); // throw an Error
});
};
Run Code Online (Sandbox Code Playgroud)
然而,内部throw会立即被外部捕获.catch()并显示“没有这样的文件!” 错误消息将丢失,取而代之的是“错误:获取文档:”。
通过调整整体模式可以避免这种损失,如下所示:
// (2) ILLUSTRATIVE - NOT YET THE FULL SOLUTION
getInviteById(inviteId: string): Promise<any> {
var inviteDocument = this.afs.collection('invites').doc(inviteId);
return inviteDocument.ref.get()
.catch(error => { // .catch() error arising from inviteDocument.ref.get()
throw new Error('Error: Getting document:');
})
.then(doc => {
if (doc.exists) {
const id = doc.id;
var data = doc.data() as any;
return {id, ...data};
} else {
throw new Error('No such document!'); // can now be caught only by getInviteById's caller
}
});
};
Run Code Online (Sandbox Code Playgroud)
然而,即使这样仍然不正确,因为存在以下可能性:
this.afs.collection('invites').doc(inviteId)可能会返回null,在这种情况下应该抛出错误。this.afs.collection('invites').doc(inviteId)或者可能会同步inviteDocument.ref.get()抛出。在任何一种情况下,调用者都有权期望承诺返回函数始终异步抛出,无论错误如何/在何处出现。
该工件可以通过确保从承诺链内部执行来克服,并且var inviteDocument = this.afs.collection('invites').doc(inviteId);案例会得到适当的处理,如下所示:inviteDocument.ref.get()null
// (3) SOLUTION
getInviteById(inviteId: string): Promise<any> {
return Promise.resolve() // neutral starter promise
.then(() => {
var inviteDocument = this.afs.collection('invites').doc(inviteId); // now executed inside promise chain
if(inviteDocument) {
return inviteDocument.ref.get(); // now executed inside promise chain.
} else {
throw new Error(); // no point setting an error message here as it will be overridden below.
}
})
.catch(error => {
throw new Error('Error: Getting document:');
})
.then(doc => {
if (doc.exists) {
const id = doc.id;
var data = doc.data() as any;
return {id, ...data};
} else {
throw new Error('No such document!');
}
});
};
Run Code Online (Sandbox Code Playgroud)
调用者(您的控制器)将捕获并记录由以下原因引起的任何错误getInviteById():
this.inviteService.getInviteById(inviteId)
.then(result => { // better not to name the variable `resolve`
this.invite = result;
})
.catch(err => {
console.log("INVITE-COMPONENT: Cannot get invite for this id: " + error.message);
});
Run Code Online (Sandbox Code Playgroud)
笔记
console.log()内部是不必要的getInviteById()(调试时可能除外)。调用者.catch()将进行所有必要的日志记录。| 归档时间: |
|
| 查看次数: |
10196 次 |
| 最近记录: |