0
votes

I'm trying to upload 2 functions to Firebase Cloud functions but it always ends up giving an error saying:

"Error: Functions did not deploy properly."

When I go to the Firebase Cloud Functions Logs this is the only Info I get:

{"@type":"type.googleapis.com/google.cloud.audit.AuditLog","status":{"code":3,"message":"Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation."},"authenticationinfo":{"principalemail":"[email protected]"},"servicename":"cloudfunctions.googleapis.com","methodname":"google.cloud.functions.v1.cloudfunctionsservice.updatefunction","resourceName":"projects/xxxxxxxxx-ad581/locations/us-central1/functions/newUserSignup"}

Now in here it says there is a bug on my Code but I can't seem to find it:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
const randomstring = require("randomstring");

admin.initializeApp();

exports.newUserSignup = functions.auth.user().onCreate((user) => {
  const provid = user.providerData[0].providerId;
  switch (provid) {
    case "facebook.com": {
      let username;
      const tokens = user.displayName.split(" ");
      const nameLength = tokens.length;
      if (nameLength > 1) {
        username = tokens[0] + randomstring.generate(12) + tokens[1];
      } else {
        username = tokens[0] + randomstring.generate(17);
      }
      return admin.firestore().collection("users").doc(user.uid).set({
        "username": username,
        "email": user.email,
        "image_url": user.photoURL,
        "name": user.displayName,
      });
    }
    case "google.com": {
      let username;
      const tokens = user.displayName.split(" ");
      const nameLength = tokens.length;

      if (nameLength > 1) {
        username = tokens[0] + randomstring.generate(12) + tokens[1];
      } else {
        username = tokens[0] + randomstring.generate(17);
      }
      return admin.firestore().collection("users").doc(user.uid).set({
        "username": username,
        "email": user.email,
        "image_url": user.photoURL,
        "name": user.displayName,
      });
    }
    case "apple.com": {
      return admin.firestore().collection("users").doc(user.uid).set({
        "username": null,
        "email": user.email,
        "image_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Apple_logo_black.svg/647px-Apple_logo_black.svg.png",
        "name": null,
      });
    }
    case "password": {
      return admin.firestore().collection("users").doc(user.uid).set({
        "username": null,
        "email": user.email,
        "image_url": null,
        "name": null,
      });
    }
    default: {
      return;
    }
  }
});

exports.userDeleted = functions.auth.user().onDelete((user) => {
  const doc = admin.firestore().collection("users").doc(user.uid);
  return doc.delete();
});

Basically what I'm trying to do is that with the first Function newUserSignup is that when a new user access it gets the providerId which I save to the variable provid and then do a switch where if facebook.com is the provider has some logic, or changes the logic depending on the provider.

On the second Function userDeleted I only want to delete the user entry from the the Firestore when the user deleted.

Additional Information

The part of Collection with Double Quotes was requested by ESLint, the randomstring is from a package call RandomString on NPM https://www.npmjs.com/package/randomstring this is to generate a random hash to my usernames. Also the Whole project is already Firebase Init so it has the .firebaserc for the connection.

Any Ideas what the Bug might be?

Kind Regards

1
How are your functions being invoked, by http request, or Firebase trigger?Brettski
Firebase TriggerRene Alas

1 Answers

0
votes

I finally found out what the problem is:

Explorer

Inside the project, I had 2 node modules in the folder, one outside and one inside the Functions Folder, and I use a node module (RandomString), in the functions, so I needed to add the npm install inside the Functions folder.

Sorry for the question Guys.

Kind Regards.