繁体   English   中英

从 Firestore 获取文档时的错误处理

[英]Error handling when getting document from 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;
};

控制器:

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." );
    });

如果 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
    });
};

但是,内部throw将立即被外部.catch()捕获,并且“No such document:”错误消息将丢失,取而代之的是“Error: Getting document.”。

可以通过如下调整整体模式来避免这种损失:

// (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
        }
    });
};

但是,即使这样还不正确,因为存在以下可能性:

  1. this.afs.collection('invites').doc(inviteId)可能返回null ,在这种情况下应该抛出错误。
  2. this.afs.collection('invites').doc(inviteId)inviteDocument.ref.get()可能会同步抛出。

在任何一种情况下,调用者都有权期望返回 promise 的函数始终异步抛出错误,而不管错误是如何/在何处出现的。

这个工件可以通过确保var inviteDocument = this.afs.collection('invites').doc(inviteId);来克服。 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!');
        }
    });
};

调用者(您的控制器)将捕获并记录由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);
});

笔记

  1. console.log()getInviteById() ) 中是不必要的(调试时可能除外)。 调用者的.catch()将执行所有必要的日志记录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM