0

I am an android developer with little to no experience in web. I wrote a cloud function to register user. but is too nested. I know that I can use promise chaining or async/await. when i tried to use async vs code gives error that, it cannot find name, async, the target is ES6. when i tried chaining promises, it is giving warnings like, not all code paths returns a value. This is my code

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

this is what i tried when changing to promise chaining, but is showing warning that 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.`
                }
            );
        });
});

can anyone help me to re-factor this code to use async/await of promise chaining, so that it can be more readable.

6
  • I don't see any async/await in your code. Commented Mar 5, 2018 at 19:13
  • i reverted it back to this state. this code is working as intended, but it is too nested. that is why i want to refactor it to use async/await or promise chaining Commented Mar 5, 2018 at 19:17
  • So you're saying you want someone else to do this work for you, rather than get help for what you might have done wrong on your own? Commented Mar 5, 2018 at 19:21
  • i am just asking for help, I can post that promise chaining function i wrote with all those errors, but that is also a little big like the function i already posted and it is not working too. that is why i didn't include that in this question Commented Mar 5, 2018 at 19:24
  • Learning how to interpret warnings and errors is very helpful, so next time you encounter it, you can fix it yourself. I suspect you'd be better helped by understanding what went wrong with your own work, and it would help others reading here that encounter the same error messages, which should be easy to search. Commented Mar 5, 2018 at 19:32

1 Answer 1

1

Without knowing what you tried, I'm not sure why you got the error in the first place, but a simple transformation of your code to use async/await would be:

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

Note Your code is a pretty big chuck and without more context the code above may contain errors, but this should get you started.

Sign up to request clarification or add additional context in comments.

1 Comment

I was editing my question to add what I have tried while you are posting your answer. Thanks for the help with the little information i provided (my bad) . I will update you soon. and thanks for your time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.