Search code examples
angularfirebasefirebase-authenticationgoogle-cloud-functionsangularfire2

Cloud functions: oncall function returns null before running chain of promises


I created a cloud function to create a firebase auth user and to record user's data into firestore.

Cloud Function

const admin = require('firebase-admin');
admin.initializeApp();

exports.createUser = functions.https.onCall((data, response) => {
  admin.auth().createUser({
    email: data.email,
    password: data.password,
    displayName: data.name
  }).then(user => {
    return admin.firestore().collection('users').doc(user.uid).set({
      email: data.email,
      id: user.uid,
      name: data.name,
      role: data.role
    });
  }).then(() => {
    return { status: true, message: 'User added successfully!' };
  }).catch(error => {
    return { status: false, message: 'Error occurred while creating the user!' };
  })
})

Angular .ts file

export class AddUserComponent implements OnInit {
  constructor(private angularFireAuth: AngularFireAuth) {}

  ngOnInit(): void {
  }

  onSubmit(user) {
    this.angularFireFunctions.httpsCallable('createUser')(user).subscribe(response => {
      console.log(response);
    }, error => {
      console.log(error);
    });
  }
}

When I call this function in my angular app using angular/fire it returns null. Also, it takes upto 3 minutes to add user to firestore. What should I modify in this code?


Solution

  • As in the firebase docs on sending back the result for onCall funtions here, you should return any asynchronous function. So, add return keyword before admin.auth()... in the cloud function.