Dasser Basyouni
Dasser Basyouni

Reputation: 3252

Cloud Function - Inserting data to admin.firestore two times in onCreate(user) is not working

I'm using Cloud function to insert user data in FireStore in two different documents, the first insert of phoneNumber/email (userInfoRef) is working well while the insert of uid (searchRef) is not working.

Also, cloud function logs don't return any error just

DEBUG: Billing account not configured..
INFO:  Function execution started
INFO:  Billing account not configured.. //again
DEBUG: new user is here //(my written log)
DEBUG: null//(my written log)
DEBUG: Function execution took 2892 ms, finished with status: 'ok'

Here is my code

import * as functions from 'firebase-functions';

exports.saveUserDataToDatabase = functions.auth.user().onCreate((user) => {
    console.log('new user is here');
    console.log(user.photoURL);

    const email = user.email;
    const phoneNumber = user.phoneNumber;
    const uid = user.uid;

    const admin = require("firebase-admin");
    admin.initializeApp();
    const db = admin.firestore();
    const settings = {timestampsInSnapshots: true};
    db.settings(settings);

    const userInfoRef = db.collection("users");

    if (email === null){
        userInfoRef.doc(uid).set({
            "phone": phoneNumber
        });

        const searchRef = db.collection("Search_PhoneByUid");
        searchRef.doc(phoneNumber).set({
            "uid": uid
        });

        return true;

    } else if (phoneNumber === null) {
         userInfoRef.doc(uid).set({
            "email": email
        });

        const searchRef = db.collection("Search_EmailByUid");
        searchRef.doc(email).set({
            "uid": uid
        });

        return true;
    }
    return false;

});

Upvotes: 1

Views: 464

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317372

You're not returning a promise that's resolved when all of the async work is complete in your function. You're ignoring all the promises returned by all the calls to set(). If you don't return a promise, Cloud Functions may shut down your function before the async work is complete.

See the documentation for more information.

Upvotes: 2

Related Questions