[英]Error handling when getting document from Firestore
In Angular 5 with FireStore and angularfire2, what is the correct way to handle errors when getting a document from a service via a controller?在带有 FireStore 和 angularfire2 的 Angular 5 中,通过控制器从服务获取文档时处理错误的正确方法是什么?
Service:服务:
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;
};
Controller:控制器:
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." );
});
All works well IF a document with the invite-id exists in FireStore.如果 FireStore 中存在具有邀请 ID 的文档,则一切正常。 However, if there is no document for the invite id in FireStore, then the service will log "Error: No such document," (as expected), BUT the component will not go into its own
catch
case.但是,如果 FireStore 中没有邀请 ID 的文档,则服务将记录“错误:没有此类文档”(如预期的那样),但组件不会进入其自己的
catch
案例。
How can I process the "no such document" error in my component , so that I can modify my UI accordingly?我如何处理我的组件中的“没有这样的文档”错误,以便我可以相应地修改我的 UI?
You can return a rejected promise but it's simpler to throw
.您可以返回一个被拒绝的承诺,但
throw
更简单。
So, straightforwardly, you might write:所以,直截了当地说,你可以这样写:
// (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
});
};
HOWEVER, the inner throw
would be immediately caught by the outer .catch()
and the 'No such document:' error message would be lost in favour of 'Error: Getting document.'.但是,内部
throw
将立即被外部.catch()
捕获,并且“No such document:”错误消息将丢失,取而代之的是“Error: Getting document.”。
That loss can be avoided by adjusting the overall pattern as follows:可以通过如下调整整体模式来避免这种损失:
// (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
}
});
};
HOWEVER, even that isn't yet correct because the possibilities exist that:但是,即使这样还不正确,因为存在以下可能性:
this.afs.collection('invites').doc(inviteId)
might return null
, in which case an error should be thrown. this.afs.collection('invites').doc(inviteId)
可能返回null
,在这种情况下应该抛出错误。this.afs.collection('invites').doc(inviteId)
or inviteDocument.ref.get()
might throw synchronously . this.afs.collection('invites').doc(inviteId)
或inviteDocument.ref.get()
可能会同步抛出。 In either case, the caller has a right to expect a promise-returning function always to throw asynchronously regardless of how/where the error arose.在任何一种情况下,调用者都有权期望返回 promise 的函数始终异步抛出错误,而不管错误是如何/在何处出现的。
That artifact can be overcome by ensuring var inviteDocument = this.afs.collection('invites').doc(inviteId);
这个工件可以通过确保
var inviteDocument = this.afs.collection('invites').doc(inviteId);
来克服。 and inviteDocument.ref.get()
are executed from inside the promise chain and the null
case is handled appropriately, as follows:和
inviteDocument.ref.get()
从 promise 链内部执行,并且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!');
}
});
};
The caller (your controller) will catch and log any error arising from getInviteById()
:调用者(您的控制器)将捕获并记录由
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);
});
Notes笔记
console.log()
is unnecessary inside getInviteById()
(except possibly while debugging). console.log()
在getInviteById()
) 中是不必要的(调试时可能除外)。 The caller's .catch()
will do all the necessary logging..catch()
将执行所有必要的日志记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.