【问题标题】:Refactoring typescript cloud function to use promise chaining重构打字稿云功能以使用承诺链
【发布时间】:2018-08-13 12:58:57
【问题描述】:

我是一名 Android 开发人员,几乎没有网络经验。我写了一个云函数来注册用户。但是太嵌套了。我知道我可以使用承诺链或异步/等待。当我尝试使用异步 vs 代码时,会出现错误,它cannot find name, async,目标是 ES6。当我尝试链接承诺时,它会发出警告,例如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.`
                                    }
                                );
                            });
                        }
                    });
            }
        });
});

这是我在更改为承诺链接时尝试过的,但显示警告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 的 Promise 链,使其更具可读性。

【问题讨论】:

  • 我在您的代码中没有看到任何 async/await。
  • 我把它恢复到这个状态。此代码按预期工作,但它过于嵌套。这就是为什么我想重构它以使用 async/await 或 Promise 链接
  • 所以您是说您希望其他人为您完成这项工作,而不是为您自己可能做错的事情寻求帮助?
  • 我只是在寻求帮助,我可以发布我写的带有所有这些错误的承诺链函数,但这也有点像我已经发布的函数,它也不起作用。这就是为什么我没有在这个问题中包含它
  • 学习如何解释警告和错误非常有帮助,所以下次遇到它,你可以自己修复它。我怀疑通过了解自己的工作出了什么问题会更好地帮助您,这将有助于其他阅读此处遇到相同错误消息的人,这应该很容易搜索。

标签: typescript firebase async-await google-cloud-functions es6-promise


【解决方案1】:

在不知道您尝试了什么的情况下,我不确定您为什么首先会遇到错误,但是您的代码的简单转换为使用 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.`
                }
            );
        }
    }
});

注意您的代码相当大,如果没有更多上下文,上面的代码可能包含错误,但这应该可以帮助您入门。

【讨论】:

  • 我正在编辑我的问题以添加我在您发布答案时尝试过的内容。感谢您对我提供的少量信息的帮助(我的错误)。我会尽快更新你。并感谢您的宝贵时间
猜你喜欢
  • 2023-04-01
  • 2018-04-19
  • 2021-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
相关资源
最近更新 更多