簡體   English   中英

重構打字稿雲功能以使用承諾鏈

[英]Refactoring typescript cloud function to use promise chaining

我是一位Android開發人員,幾乎沒有網絡經驗。 我寫了一個雲功能來注冊用戶。 但是太嵌套了。 我知道我可以使用諾言鏈或異步/等待。 當我嘗試使用異步vs代碼給出錯誤消息時,它cannot find name, async ,目標是ES6。 當我嘗試鏈接promises時,它發出警告,例如, not all code paths returns a value 這是我的代碼

exports.register = functions.https.onRequest((request, response) => {
    const db = admin.firestore();
    const user: string = request.body['username'];
    const phone: number = request.body['phone'];
    const password: string = request.body['password'];

    return db.collection('rejectedContacts').where('contact', '==', phone).get()
        .then(rejectedContactsSnapShot => {
            if (rejectedContactsSnapShot.size > 0) {
                return response.json(
                    {
                        status: 0,
                        message: `Contact, ${phone} is blocked, please try again with another number`,
                        result: null
                    }
                );
            } else {
                return db.collection('users').where('contacts.phone', '==', phone).get()
                    .then(contactsSnapShot => {
                        if (contactsSnapShot.size > 0) {
                            return response.json(
                                {
                                    status: 0,
                                    message: `Contact, ${phone} is already assigned with an account. Did you forgot your pasword?`,
                                    result: null
                                }
                            );
                        } else {
                            return db.collection('users').add(
                                {
                                    user: user,
                                    password: password,
                                    isBlocked: false,
                                    joiningDate: Date.now(),
                                    phoneVerified: false,
                                    deleted: false,
                                    contacts:
                                        {
                                            phone: phone
                                        }

                                }
                            ).then((writeResult) => {
                                return response.json(
                                    {
                                        result: `User with ID: ${writeResult.id} added.`
                                    }
                                );
                            });
                        }
                    });
            }
        });
});

這是我更改為Promise鏈接時嘗試的方法,但顯示警告, not all code paths return a value

exports.register = functions.https.onRequest((request, response) => {
    const db = admin.firestore();

    const user: string = request.body['username'];
    const phone: number = request.body['phone'];
    const password: string = request.body['password'];

    return db.collection('rejectedContacts').where('contact', '==', phone).get()
        .then(rejectedContactsSnapShot => {
            if (rejectedContactsSnapShot.size > 0) {
                return response.json(
                    {
                        status: 0,
                        message: `Contact, ${phone} is blocked, please try again with another number`,
                        result: null
                    }
                );
            } 
        }).then(notRejected=>{
            return db.collection('users').where('contacts.phone', '==', phone).get()
                    .then(contactsSnapShot => {
                        if (contactsSnapShot.size > 0) {
                            return response.json(
                                {
                                    status: 0,
                                    message: `Contact, ${phone} is already assigned with an account. Did you forgot your pasword?`,
                                    result: null
                                }
                            );
                        } 
                    });
        }).then(numberDoesNotExists=>{
            return db.collection('users').add(
                {
                    user: user,
                    password: password,
                    isBlocked: false,
                    joiningDate: Date.now(),
                    phoneVerified: false,
                    deleted: false,
                    contacts:
                        {
                            phone: phone
                        }

                }
            );
        }).then((writeResult) => {
            return response.json(
                {
                    result: `User with ID: ${writeResult.id} added.`
                }
            );
        });
});

誰能幫助我重構此代碼以使用承諾鏈的異步/等待,以便使其更具可讀性。

不知道您嘗試了什么,我不確定為什么會首先出現錯誤,但是使用async / await進行代碼的簡單轉換將是:

functions.https.onRequest(async (request, response) => {
    const db = admin.firestore();
    const user: string = request.body['username'];
    const phone: number = request.body['phone'];
    const password: string = request.body['password'];

    let rejectedContactsSnapShot = await db.collection('rejectedContacts').where('contact', '==', phone).get();
    if (rejectedContactsSnapShot.size > 0) {
        return response.json(
            {
                status: 0,
                message: `Contact, ${phone} is blocked, please try again with another number`,
                result: null
            }
        );
    } else {
        let contactsSnapShot = await db.collection('users').where('contacts.phone', '==', phone).get();
        if (contactsSnapShot.size > 0) {
            return response.json(
                {
                    status: 0,
                    message: `Contact, ${phone} is already assigned with an account. Did you forgot your pasword?`,
                    result: null
                }
            );
        } else {
            let writeResult = await db.collection('users').add({
                user: user,
                password: password,
                isBlocked: false,
                joiningDate: Date.now(),
                phoneVerified: false,
                deleted: false,
                contacts:{
                    phone: phone
                }
            })

            return response.json(
                {
                    result: `User with ID: ${writeResult.id} added.`
                }
            );
        }
    }
});

注意您的代碼是一個很大的問題,沒有更多上下文,上面的代碼可能包含錯誤,但這應該可以幫助您入門。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM