Firebase cloud function returns null to client even though it succeeded

Viewed 75

I've been try to write my first cloud function, which you can see below. In the cloud function log, it outputs that it hits the first console.log(Successful: userRecord.uid) but doesn't seem to do anything after it.

Could anybody point me in the right direction as to what is wrong?

Thank you very much.

const admin = require('firebase-admin');
const express = require('express');
const cors = require('cors');

const app = express();
admin.initializeApp();
app.use(cors({origin: true}));
const db = admin.firestore();

exports.addUser = functions.https.onCall((data, res) =>
{
    console.time();
    admin.auth().createUser({
        email: data.email,
        emailVerified: true,
        password: data.password,
        disabled: false
    })
        .then(userRecord => {
            console.log('Successful: ', userRecord.uid);
            const message = {
                firstname: data.firstname,
                lastname: data.lastname
            }
            return db.collection('Users').add(message)
                .then(() => {
                    return "Success";
                })
        })
        .catch(function(error) {
            console.log('Error creating new user:', error);
            console.trace();
            console.timeEnd();
            return "Error";
        });
});
1 Answers

for anyone wondering, I was able to fix my own issue:

For me, I needed to return each function, so the code became this:

Notice the return on admin.auth().createUser

const express = require('express');
const cors = require('cors');

const app = express();
admin.initializeApp();
app.use(cors({origin: true}));
const db = admin.firestore();

exports.addUser = functions.https.onCall((data, res) =>
{
    console.time();
    return admin.auth().createUser({
        email: data.email,
        emailVerified: true,
        password: data.password,
        disabled: false
    })
        .then(userRecord => {
            console.log('Successful: ', userRecord.uid);
            const message = {
                firstname: data.firstname,
                lastname: data.lastname
            }
            return db.collection('Users').add(message)
                .then(() => {
                    return "Success";
                })
        })
        .catch(function(error) {
            console.log('Error creating new user:', error);
            console.trace();
            console.timeEnd();
            return "Error";
        });
});```
Related