英文:
When you recreate your Firebase backend functions and redo the setup, does the project ignore the original set of functions?
问题
I have a Firebase project that uses some Firestore triggers and backend Typescript functions. Let's call the folder for the original set of backend functions CloudFunctions1
. This was working great. Recently, I completed recreated my cloud functions, going through the whole process of doing firebase init
, firebase login
, even adding the extensions I wanted in VSCode. I did this so that my project starts with the latest base skeleton since it had been years since I started the project initially. The new folder for this is called CloudFunctions2
.
How does my app know to call the functions in CloudFunctions2
instead of the functions in CloudFunctions1
? When I did the setup, I selected a project, sure, but does that mean that that project will now completely ignore/disregard the functions in CloudFunctions1
?
index.ts:
import {onDocumentCreated} from "firebase-functions/v2/firestore";
import {onDocumentUpdated} from "firebase-functions/v2/firestore";
import {onDocumentDeleted} from "firebase-functions/v2/firestore";
import {onCall} from "firebase-functions/v2/https";
import {HttpsError} from "firebase-functions/v2/https";
exports.callGroupRTDBSetup = onCall({cors: true}, async (req) => {
try {
const groupID = req.data.groupID;
const adminUID = req.data.adminUID;
const adminUsername = req.data.adminUsername;
const createdTimestamp = req.data.createdTimestamp;
await setGroupAdminRTDB(groupID, adminUID, adminUsername, createdTimestamp);
await addMemberRTDB(groupID, adminUID, adminUsername, createdTimestamp);
return {
returnVal: 1,
returnMsg: "callGroupRTDBSetup success!",
};
} catch (error) {
throw new HttpsError("invalid-argument", "Error: " + error);
}
});
/**
* Sets the admin information into the group node in RTDB
* @param {string} groupID
* @param {string} adminUID
* @param {string} adminUsername
* @param {number} createdAdminTimestamp
* @return {Promise<{returnVal: number, returnMsg: string}>} A Promise that
* resolves with an object containing the return value and message.
*/
async function setGroupAdminRTDB(
groupID: string,
adminUID: string,
adminUsername: string,
createdAdminTimestamp: number
): Promise<{returnVal: number; returnMsg: string}> {
let returnVal = 0;
let returnMsg = "";
const nodeRef = await admin
.database()
.ref("app/groups/" + groupID + "/admin/")
.once("value");
const data = nodeRef.val();
if (data == null) {
try {
const adminData = {
adminUsername: adminUsername,
createdAdminTimestamp: createdAdminTimestamp,
};
await admin
.database()
.ref("app/groups/" + groupID + "/admin/" + adminUID)
.set(adminData);
returnVal = 1;
returnMsg = "Data successfully added!";
} catch (error) {
returnVal = 0;
returnMsg = "ERROR: " + error;
}
} else {
returnVal = 0;
returnMsg = "Error: data already exists at admin node";
}
return {returnVal: returnVal, returnMsg: returnMsg};
}
I deploy these functions using firebase deploy
.
英文:
I have a Firebase project that uses some Firestore triggers and backend Typescript functions. Let's call the folder for the original set of backend functions CloudFunctions1
. This was working great. Recently, I completed recreated my cloud functions, going through the whole process of doing firebase init
, firebase login
, even adding the extensions I wanted in VSCode. I did this so that my project starts with the latest base skeleton since it had been years since I started the project initially. The new folder for this is called CloudFunctions2
.
How does my app know to call the functions in CloudFunctions2
instead of the functions in CloudFunctions1
? When I did the setup, I selected a project, sure, but does that mean that that project will now completely ignore/disregard the functions in CloudFunctions1
?
index.ts:
import {onDocumentCreated} from "firebase-functions/v2/firestore";
import {onDocumentUpdated} from "firebase-functions/v2/firestore";
import {onDocumentDeleted} from "firebase-functions/v2/firestore";
import {onCall} from "firebase-functions/v2/https";
import {HttpsError} from "firebase-functions/v2/https";
exports.callGroupRTDBSetup = onCall({cors: true}, async (req) => {
try {
const groupID = req.data.groupID;
const adminUID = req.data.adminUID;
const adminUsername = req.data.adminUsername;
const createdTimestamp = req.data.createdTimestamp;
await setGroupAdminRTDB(groupID, adminUID, adminUsername, createdTimestamp);
await addMemberRTDB(groupID, adminUID, adminUsername, createdTimestamp);
return {
returnVal: 1,
returnMsg: "callGroupRTDBSetup success!",
};
} catch (error) {
throw new HttpsError("invalid-argument", "Error: " + error);
}
});
/**
* Sets the admin information into the group node in RTDB
* @param {string} groupID
* @param {string} adminUID
* @param {string} adminUsername
* @param {number} createdAdminTimestamp
* @return {Promise<{returnVal: number, returnMsg: string}>} A Promise that
* resolves with an object containing the return value and message.
*/
async function setGroupAdminRTDB(
groupID: string,
adminUID: string,
adminUsername: string,
createdAdminTimestamp: number
): Promise<{returnVal: number; returnMsg: string}> {
let returnVal = 0;
let returnMsg = "";
const nodeRef = await admin
.database()
.ref("app/groups/" + groupID + "/admin/")
.once("value");
const data = nodeRef.val();
if (data == null) {
try {
const adminData = {
adminUsername: adminUsername,
createdAdminTimestamp: createdAdminTimestamp,
};
await admin
.database()
.ref("app/groups/" + groupID + "/admin/" + adminUID)
.set(adminData);
returnVal = 1;
returnMsg = "Data successfully added!";
} catch (error) {
returnVal = 0;
returnMsg = "ERROR: " + error;
}
} else {
returnVal = 0;
returnMsg = "Error: data already exists at admin node";
}
return {returnVal: returnVal, returnMsg: returnMsg};
}
I deploy these functions using firebase deploy
.
答案1
得分: 1
"firebase deploy" 将简单地使用新的函数覆盖所有先前部署的函数,一旦执行完成,无论您在哪里运行该命令。旧函数将被替换,新函数立即生效。客户端不能选择调用旧版本。
英文:
firebase deploy
will simply overwrite all previously deployed functions with the new functions by the time it's done executing. It doesn't matter where your run the command. The old functions are gone and the new ones take their place immediately. The client cannot choose to invoke the old versions.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论