简体   繁体   English

我可以制作一个仅适用于本地 Firebase 模拟器的云 Function 吗?

[英]can I make a Cloud Function that only for local Firebase Emulators?

I need to make a function that will be used only for Firebase emulator.我需要制作一个仅用于 Firebase 仿真器的 function。 I need to perform some actions to my Firestore emulators, to populate Firestore emulators using dummy data.我需要对我的 Firestore 模拟器执行一些操作,以使用虚拟数据填充 Firestore 模拟器。

I write this code in my index.ts我在我的index.ts中写了这段代码

exports.onlyForFirebaseSimulator = functions.https.onRequest(async (req, res) => {
  
  
});

I want to make this function available when I run firebase emulators:start , but it will not be deployed to production when I run firebase deploy .我想让这个 function 在我运行firebase emulators:start时可用,但是当我运行firebase deploy时它不会部署到生产中。

I have to comment it out manually before deployment.我必须在部署之前手动将其注释掉。 I don't want to manually set it, because it is error prone.我不想手动设置它,因为它容易出错。

If you are deploying with the Firebase CLI deploy command you could select only the functions to deploy to production by specifying the --only flag as explained on the relevant sections of the docs : eg if you have three functions named addMessage, makeUppercase, and onlyForFirebaseSimulator running the following command:如果您使用Firebase CLI 部署命令进行部署,您可以 select 通过指定 --only 标志仅部署到生产环境,如文档的相关部分所述:例如,如果您有三个名为 addMessage、makeUppercase 和 onlyForFirebaseSimulator 的函数运行以下命令:

firebase deploy --only functions:addMessage,functions:makeUppercase

will not deploy your onlyForFirebaseSimulator to production.不会将您的 onlyForFirebaseSimulator 部署到生产环境。

I think I find the answer.我想我找到了答案。

I am using group functions to organize multiple functions in my index.ts , I suggest you to read it first, otherwise you will be confused to read my index.ts .我在我的index.ts中使用分组函数来组织多个函数,建议你先阅读它,否则你会混淆阅读我的index.ts I am using Typescript/ES6, but the concept will be the same我使用的是 Typescript/ES6,但概念是一样的

for 'normal' deployment, in index.ts will be like this对于“正常”部署,在index.ts中将是这样的

// Firestore triggers
export * as users from "./firestore/triggers/users/users_triggers";
export * as users_attendedEvents from "./firestore/triggers/users/attendedEvents/attended_events_triggers";
export * as users_followers from "./firestore/triggers/users/followers/followers_triggers";

// Firebase Authentication triggers
export * as auth from "./firebase_authentication/triggers/firebase_auth_triggers";

and now I want to add functions that will not be deployed on production (when I run firebase deploy ) but will be available when emulators is running (when I run firebase emulators:start)现在我想添加不会在生产中部署的功能(当我运行firebase deploy时)但在模拟器运行时可用(当我运行 firebase emulators:start 时)

to detect whether the emulators is running or not, we can check its environment variable, process.env.FUNCTIONS_EMULATOR will be true.要检测模拟器是否正在运行,我们可以检查它的环境变量, process.env.FUNCTIONS_EMULATOR是否为真。 check this answer for more info检查此答案以获取更多信息

so my full index.ts will be like this所以我的完整index.ts将是这样的

// Firestore triggers
export * as users from "./firestore/triggers/users/users_triggers";
export * as users_attendedEvents from "./firestore/triggers/users/attendedEvents/attended_events_triggers";
export * as users_followers from "./firestore/triggers/users/followers/followers_triggers";
    
// Firebase Authentication triggers
export * as auth from "./firebase_authentication/triggers/firebase_auth_triggers";

// The code below will be exported only if emulators are running, to avoid deployment to production server
const isRunningOnEmulator = (process.env.FUNCTIONS_EMULATOR && process.env.FIRESTORE_EMULATOR_HOST);
import * as emulator_functions from "./utilities/emulators/http_triggers/for_testing_via_firestore_emulators";
export const forEmulator = isRunningOnEmulator ? emulator_functions : undefined;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase 云 function 本地代码更改未反映在模拟器中 - Firebase cloud function local code changes are not reflected in emulators 使用模拟器时可以避免使用实时 Firebase 存储吗? - Can I avoid using live Firebase Storage when using emulators? 如何在 Firebase 云 Function 中进行 API 调用? - How do I make an API call inside a Firebase Cloud Function? 如何确保我的 getUserProfile 云函数在 firebase 中仅在登录用户的会话请求时才返回数据? - How do I make sure my getUserProfile cloud function in firebase only returns data if the logged in user's session requests it? Firebase 云函数服务本地文件以供下载 - Firebase Cloud Function Serving Local File for Download 本地云功能Firebase将数据添加到Firestore - Local Cloud Function Firebase add data to Firestore Firebase Cloud Function - 我该如何解决这个错误:ECONNRESET - Firebase Cloud Function - How can I solve this error: ECONNRESET 如何获取 Firebase 云功能节点中的字段值 - How can i get the values of the field in Firebase cloud function node 我怎么知道我的 firebase 云 function 是否正常工作? - How can i know that my firebase cloud function is working or not? 当我在本地运行云功能模拟器时无法运行更新的代码 - Can't run the updated code when I run cloud functions emulators on locally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM