通过一次访存(Firestore)

时间:2019-09-18 12:09:05

标签: typescript firebase google-cloud-firestore google-cloud-functions admin-sdk

我有一个用户uid。用户具有多个角色,每个角色映射到不同的组织中,并且每个组织都有运动集合。  现在,我必须检查用户角色和唯一的组织列表,然后进行运动,最后将其发送给客户端

我遇到了延迟问题。有些组织来了,有些则没有。保证等待效果不佳或我的代码错误。

使用(用户集合)中的ID获取用户详细信息

  1. 用户子集合(按季节划分的角色)-角色列表中的Organization_id为唯一(多个组织)

  2. 使用多组织列表可通过collection.doc全部获得

  3. 获得组织及其子项运动详细信息之后(子集合多个)

  4. 所有组织及其体育活动完成后,您可以将回复返回给客户 响应必须是基于角色的组织

我是Firebase的新手。如果我错了纠正我 示例代码:

adminref.collection('/users').doc(uid).get().then(async userExist => {
                if (userExist.exists) {
                    //Roles Based Organizations                    
                    await adminref.collection('/users').doc(uid).collection('/roles_by_season').where('organization_id', '>', '').get().then(async roleOrg => {
                        const roleBasedOrgList: any = roleOrg.docs.map(doc => doc.get('organization_id'));
                        let roleBasedOrgUniqueList: any = [];
                        roleBasedOrgUniqueList = roleBasedOrgList.filter((elem: any, index: any, self: any) => {
                            return index === self.indexOf(elem);
                        }) // Unique Organization for the user

                        const roleBasedOrg: any = [];
                        if (roleBasedOrgUniqueList.length !== 0) {
                            roleBasedOrgUniqueList.forEach(async (orgInfo: any, orgIndex: any) => {

                                await adminref.collection('/organization').doc(orgInfo).get().then(async organizationInfo => {
                                    const organizationDetail: any = organizationInfo.data();
                                    organizationDetail.sports = [];
                                    try {
                                        const sportsSnapshot = await adminref.collection('/organization').doc(orgInfo).collection('/sports').get()                                        
                                        organizationDetail.sports = sportsSnapshot.docs.map(doc => doc.data());
                                        if(organizationDetail.sports.length === 0) {
                                            organizationDetail.sports = [];
                                        }
                                    } catch (err) {
                                        console.log(err);
                                    }
                                    roleBasedOrg.push(organizationDetail);
                                    if (orgIndex === (roleBasedOrgUniqueList.length - 1)) {

                                        response.status(200).send({ status: true, message: 'Role Based Organization List Information', data: roleBasedOrg });
                                    }
                                }).catch(error => {
                                    console.log(error);
                                })
                            });
                        } else {
                            response.status(200).send({ status: true, message: 'There is no organization assigned to this User', 'data': roleBasedOrg });
                        }
                    }).catch(error => {
                        console.log(error);
                    })
                } else {
                    response.status(200).send({ status: false, message: 'No data available for this Uid' });
                }
            }).catch(error => {
                response.status(200).send({ status: false, message: error.message });
                console.log(error);
            })

Json示例:

{
    "status": true,
    "message": "Role Based Organization List Information",
    "data": [
        {

            "organization_id": "AFIEflH0XBiAEPZuBdoB",            
            "name": "Organization 1",            
            "sports": [
                {
                    "name": "Foot Ball",
                    "sport_id": "foot_ball",
                    "updated_uid": "",
                    "created_uid": "",
                    "created_datetime": "2019-09-11T04:00:00.000Z",
                    "updated_datetime": "2019-09-11T04:00:00.000Z"
                },
                {
                    "updated_uid": "",
                    "created_uid": "",
                    "created_datetime": "2019-09-11T04:00:00.000Z",
                    "updated_datetime": "2019-09-11T04:00:00.000Z",
                    "name": "Ice Hockey",
                    "sport_id": "ice_hockey"
                }
            ]
        },
        {

            "name": "Minnesota Made",
            "organization_id": "etYHyjCu9R5boNq2Fc4p",
            "sports": [
                {
                    "sport_id": "ice_hockey",
                    "updated_uid": "",
                    "created_uid": "",
                    "created_datetime": "2019-09-11T04:00:00.000Z",
                    "updated_datetime": "2019-09-11T04:00:00.000Z",
                    "name": "Ice Hockey"
                },
                {
                    "sport_id": "lacrosse",
                    "updated_uid": "",
                    "created_uid": "",
                    "created_datetime": "2019-09-11T04:00:00.000Z",
                    "updated_datetime": "2019-09-11T04:00:00.000Z",
                    "name": "Lacrosse"
                }
            ]
        }
    ]
}

通过对云功能Admin SDK的promise async / await,给我任何更好的解决方案

0 个答案:

没有答案