【发布时间】:2021-08-26 21:57:22
【问题描述】:
我正在尝试将 2 个函数上传到 Firebase Cloud 函数,但它总是最终给出错误提示:
“错误:函数未正确部署。”
当我访问 Firebase Cloud Functions 日志时,这是我得到的唯一信息:
{"@type":"type.googleapis.com/google.cloud.audit.AuditLog","status":{"code":3,"message":"函数加载用户代码失败。这是可能是由于用户代码中的错误。错误消息:错误:请检查您的功能日志以查看错误原因:https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs。可以在https://cloud.google.com/functions/docs/troubleshooting#logging 找到其他故障排除文档。请访问https://cloud.google.com/functions/docs/troubleshooting 了解详细信息故障排除文档。"},"authenticationinfo":{"principalemail":"xxxxxxx@gmail.com"},"servicename":"cloudfunctions.googleapis.com","methodname":"google.cloud.functions.v1.cloudfunctionsservice .updatefunction","resourceName":"projects/xxxxxxxxx-ad581/locations/us-central1/functions/newUserSignup"}
现在这里说我的代码有一个错误,但我似乎找不到它:
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();
});
基本上我要做的是,第一个功能 newUserSignup 是当新用户访问时,它会获得 providerId我将其保存到变量 provid 中,然后进行切换,如果 facebook.com 是提供者有一些逻辑,或者根据提供者更改逻辑。
关于第二个功能userDeleted我只想在用户删除时从Firestore中删除用户条目。
附加信息
带有双引号的集合部分是由 ESLint 请求的,随机字符串来自 NPM https://www.npmjs.com/package/randomstring 上的包调用 RandomString,这是为我的用户名生成随机哈希。此外,整个项目已经是 Firebase Init,因此它具有用于连接的 .firebaserc。
任何想法可能是什么错误?
亲切的问候
【问题讨论】:
-
如何通过 http 请求或 Firebase 触发器调用您的函数?
-
Firebase 触发器
标签: firebase google-cloud-firestore google-cloud-functions