Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase admin.auth().getUser(uid) hangs (NodeJS)

I'm using admin.auth().getUser(uid) in the Firebase Admin SDK (NodeJS), in a Serverless project, and it does successfully return a result. But even though my function returns the result, my lambda still doesn't terminate and I have to use CTRL+C to end it.

Here's the full code of my function (in TypeScript):

public getUser(uid: string): any {
  console.log('FirebaseManager getUser method start');
  const self: FirebaseManager = this;
  const promise: any = self.getDeferred();

  admin.auth().getUser(uid)
  .then(function(userRecord: admin.auth.UserRecord) {
    console.log("Successfully fetched user data:", userRecord);
    promise.resolve(userRecord);
  })
  .catch(function(error: FirebaseError) {
    console.log("Error fetching user data:", error.errorInfo);
    promise.reject('Error getting Firebase user ' + uid);
  });

  return promise.promise;
}

Is there something I'm doing wrong?

Note that if I comment out the admin.auth().getUser(uid) block (and replace it with promise.resolve("ok")), my function does terminate properly. I mean, this doesn't hang (but it's a bit useless ^^):

public getUser(uid: string): any {
  console.log('FirebaseManager getUser method start');
  const self: FirebaseManager = this;
  const promise: any = self.getDeferred();

  promise.resolve("ok");

  return promise.promise;
}

I'm using Serverless 1.21.1, Typescript 2.5.2, Node 6.11.3 or 8.4.0 (2 different dev environments, both same result)

like image 848
user14764 Avatar asked Sep 12 '17 10:09

user14764


1 Answers

Apparently, you need to run admin.app().delete();, otherwise the connection to Firebase remains active, preventing the process from terminating.

So in my example code, considering that I will not need to use Firebase anymore after running this function:

public getUser(uid: string): any {
  console.log('FirebaseManager getUser method start');
  const self: FirebaseManager = this;
  const promise: any = self.getDeferred();

  admin.auth().getUser(uid)
  .then(function(userRecord: admin.auth.UserRecord) {
    console.log("Successfully fetched user data:", userRecord);
    admin.app().delete();
    promise.resolve(userRecord);
  })
  .catch(function(error: FirebaseError) {
    console.log("Error fetching user data:", error.errorInfo);
    admin.app().delete();
    promise.reject('Error getting Firebase user ' + uid);
  });

  return promise.promise;
}
like image 81
user14764 Avatar answered Oct 21 '22 22:10

user14764