title: Why does a Cloud Function for Firebase (deployed without errors), throw "Uncaught (in promise) FirebaseError: internal" when called?
body: <p>I am making an onCall function to do crud operations with the admin-sdk, create, read, update and delete users, and customClaims, but when calling the deployed function it throws the following errors in the browser console (the client is web):</p>
  <p><em>POST http://localhost:5001//southamerica-east1/usersCrud net::ERR_CONNECTION_REFUSED</em></p>
  <p><em>Uncaught (in promise) FirebaseError: internal</em></p>
  <p>This is the function code:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const functions = require("firebase-functions");
  const admin = require("firebase-admin");
  
  admin.initializeApp();
  
  exports.usersCrud = functions
    .region("southamerica-east1")
    .https.onCall((data, context) =&gt; {
      if (!context.auth || !context.auth.token.rol === "Supervisor") {
        throw new functions.https.HttpsError("Permission denied", "ERROR6", {
          error,
        });
      }
  
      switch (data.operation) {
        case "get":
          return listAllUsers()
            .then((listUsersResult) =&gt; {
              let users = [];
              listUsersResult.users.forEach((userRecord) =&gt; {
                users.push(userRecord.toJSON());
              });
              return users;
            })
            .catch((error) =&gt; {
              throw new functions.https.HttpsError(
                "Error getting users",
                "ERROR0",
                { error }
              );
            });
          break;
  
        case "del":
          return deleteUser(data.uid);
          break;
  
        case "cre":
          return createUser(data.user);
          break;
  
        case "upd":
          return updateUser(data.uid, data.user);
          break;
  
        default:
          throw new functions.https.HttpsError("Operation wrong", "ERROR5");
          break;
      }
    });
  
  const listAllUsers = async () =&gt; {
    return await admin.auth().listUsers(20);
  };
  
  const deleteUser = async (uid) =&gt; {
    return await admin
      .auth()
      .deleteUser(uid)
      .then(() =&gt; {
        return { deletedUser: uid };
      })
      .catch((error) =&gt; {
        throw new functions.https.HttpsError("Error deleting user", "ERROR1", {
          error,
        });
      });
  };
  
  const createUser = async (newUserData) =&gt; {
    const { displayName, email, rol, sucursal } = newUserData;
  
    if (!displayName || !email || !rol || !sucursal) {
      return { message: "Missing fields" };
    }
    try {
      const { uid } = await admin.auth().createUser({ displayName, email });
      await admin.auth().setCustomUserClaims(uid, { rol, sucursal });
      return { createdUser: uid };
    } catch (error) {
      throw new functions.https.HttpsError("Error creating user", "ERROR3", {
        error,
      });
    }
  };
  
  const updateUser = async (uid, updatedUser) =&gt; {
    const { displayName, email } = updatedUser;
    return await admin
      .auth()
      .updateUser(uid, { displayName, email })
      .then((userRecord) =&gt; {
        return admin
          .auth()
          .setCustomUserClaims(uid, {
            rol: updatedUser.rol,
            sucursal: updatedUser.sucursal,
          })
          .then(() =&gt; {
            return { updatedUser: userRecord.toJSON() };
          });
      })
      .catch((error) =&gt; {
        throw new functions.https.HttpsError("Error updating user", "ERROR4", {
          error,
        });
      });
  };</code></pre>
  </div>
  </div>
  </p>
  <p>I don't think it's an error in the code itself, because with the Emulator Suite it works correctly,
  I think it's more an error with the admin sdk or credentials but I'm not sure. I obviously set the GOOGLE_APPLICATION_CREDENTIALS environment variable before deploying the function, but calling it on the client gives me the same error</p>
  <p>I need help</p>