如何在部署 Firebase 云函数(第二代)时设置区域

huangapple go评论56阅读模式
英文:

How to to set Region when deploying Firebase Cloud Functions (2nd gen)

问题

I am setting up a Firebase project with functions organised in separate files. I am trying this out with 2nd gen functions.

Looking at the documentation, it seems like it has not been updated for 2nd gen as the example code functions are 1st gen:
https://firebase.google.com/docs/functions/organize-functions?gen=2nd#write_functions_in_multiple_files

Even so, everything is working fine using this simple setup:

index.ts:

import { initializeApp } from 'firebase-admin/app'
import addAmessage from './api/addAmessage'
import app from './onCall/app'

initializeApp()

exports.addAmessage = addAmessage

exports.app = app

And in the addAmessage.ts file I use the Firebase example function only adding the setGlobalOptions:

api/addAmessage.ts:

import { onRequest } from 'firebase-functions/v2/https'
import { getFirestore } from 'firebase-admin/firestore'
import { setGlobalOptions } from 'firebase-functions/v2'

setGlobalOptions({ region: 'europe-west3' })
// Take the text parameter passed to this HTTP endpoint and insert it into
// Firestore under the path /messages/:documentId/original
const addAmessage = onRequest(async (req, res) => {
  // Grab the text parameter.
  const message = req.query.text

  if (message !== undefined) {
    // Push the new message into Firestore using the Firebase Admin SDK.
    const writeResult = await getFirestore()
      .collection('messages')
      .add({ message })
    // Send back a message that we've successfully written the message
    res.json({ result: `Message with ID: ${ writeResult.id } added.` })
  }
})

export default addAmessage

With just one Function this works fine, but since I set use the setGlobalOptions in my other functions files also I get a warning in the emulator console:

"Calling setGlobalOptions twice leads to undefined behavior"

I originally put the setGlobalOptions method in my index.ts file but then it was ignored.

So, I would love to only have to setGlobalOptions once, but how is it done correctly with multiple files?

英文:

I am setting up a Firebase project with functions organised in separate files. I am trying this out with 2nd gen functions.

Looking at the documentation, it seems like it has not been updated for 2nd gen as the example code functions are 1st gen:
https://firebase.google.com/docs/functions/organize-functions?gen=2nd#write_functions_in_multiple_files

Even so, everything is working fine using this simple setup:

index.ts:

import { initializeApp } from 'firebase-admin/app'
import addAmessage from './api/addAmessage'
import app from './onCall/app'

initializeApp()

exports.addAmessage = addAmessage

exports.app = app

And in the addAmessage.ts file I use the Firebase example function only adding the setGlobalOptions:

api/addAmessage.ts:

import { onRequest } from 'firebase-functions/v2/https'
import { getFirestore } from 'firebase-admin/firestore'
import { setGlobalOptions } from 'firebase-functions/v2'

setGlobalOptions({ region: 'europe-west3' })
// Take the text parameter passed to this HTTP endpoint and insert it into
// Firestore under the path /messages/:documentId/original
const addAmessage = onRequest(async (req, res) => {
  // Grab the text parameter.
  const message = req.query.text

  if (message !== undefined) {
    // Push the new message into Firestore using the Firebase Admin SDK.
    const writeResult = await getFirestore()
      .collection('messages')
      .add({ message })
    // Send back a message that we've successfully written the message
    res.json({ result: `Message with ID: ${ writeResult.id } added.` })
  }
})

export default addAmessage

With just one Function this works fine, but since I set use the setGlobalOptions in my other functions files also I get a warning in the emulator console:

"Calling setGlobalOptions twice leads to undefined behavior"

I originally put the setGlobalOptions method in my index.ts file but then it was ignored.

So, I would love to only have to setGlobalOptions once, but how is it done correctly with multiple files?

答案1

得分: 9

我刚刚遇到了相同的问题,并通过更改导出部分来解决了它,使您的代码如下所示:

index.ts

import { initializeApp } from 'firebase-admin/app'
import { setGlobalOptions } from 'firebase-functions/v2'

initializeApp()

setGlobalOptions({ region: 'europe-west3' })

export { addAmessage } from './api/addAmessage'
export { app as import } from './onCall/app'

api/addAmessage.ts

import { onRequest } from 'firebase-functions/v2/https'
import { getFirestore } from 'firebase-admin/firestore'

// Take the text parameter passed to this HTTP endpoint and insert it into
// Firestore under the path /messages/:documentId/original
export const addAmessage = onRequest(async (req, res) => {
  // Grab the text parameter.
  const message = req.query.text

  if (message !== undefined) {
    // Push the new message into Firestore using the Firebase Admin SDK.
    const writeResult = await getFirestore()
      .collection('messages')
      .add({ message })
    // Send back a message that we've successfully written the message
    res.json({ result: `Message with ID: ${writeResult.id} added.` })
  }
})

如果需要,您仍然可以通过将选项添加到您的onRequest中来覆盖全局选项或追加它们。

export const addAmessage = onRequest({ minInstances: 1 }, async (req, res) => {

至于为什么会出现这种情况,我不知道,也许有人可以进一步解释。

英文:

I've just run into the same issue and was able to solve it by changing the export, making your code look like:

index.ts

import { initializeApp } from 'firebase-admin/app'
import { setGlobalOptions } from 'firebase-functions/v2'

initializeApp()

setGlobalOptions({ region: 'europe-west3' })

export { addAmessage } from './api/addAmessage'
export { import } app from './onCall/app'

api/addAmessage.ts

import { onRequest } from 'firebase-functions/v2/https'
import { getFirestore } from 'firebase-admin/firestore'

// Take the text parameter passed to this HTTP endpoint and insert it into
// Firestore under the path /messages/:documentId/original
export const addAmessage = onRequest(async (req, res) => {
  // Grab the text parameter.
  const message = req.query.text

  if (message !== undefined) {
    // Push the new message into Firestore using the Firebase Admin SDK.
    const writeResult = await getFirestore()
      .collection('messages')
      .add({ message })
    // Send back a message that we've successfully written the message
    res.json({ result: `Message with ID: ${ writeResult.id } added.` })
  }
})

you can still override the global options or append to them in your function if needed by adding the options to your onRequest.

export const addAmessage = onRequest({ minInstances: 1 }, async (req, res) => {

As to why this is, I do not know and maybe someone can explain it further.

答案2

得分: 0

以下是翻译好的内容:

我遇到了类似的问题,如果有人仍然在解决这个问题,您可以向可调用函数传递一个选项对象。其中一个选项是region,您可以根据需要设置它。

以下是应该有效的代码:

import { onRequest } from 'firebase-functions/v2/https'
import { getFirestore } from 'firebase-admin/firestore'

// 获取传递给此HTTP端点的文本参数,并将其插入到Firestore的路径/messages/:documentId/original下
const addAmessage = onRequest({region: 'europe-west3'}, async (req, res) => {
    // 获取文本参数
    const message = req.query.text

    if (message !== undefined) {
        // 使用Firebase Admin SDK将新消息推送到Firestore
        const writeResult = await getFirestore()
            .collection('messages')
            .add({ message })
        // 返回成功写入消息的消息
        res.json({ result: `已添加带有ID的消息:${ writeResult.id }。` })
    }
})

export default addAmessage
英文:

I had a similar issue and incase anyone is still struggling with this, you can pass an options object to the callable function. One of the options is region, which you can set as required.

Here is the code that should work :

import { onRequest } from 'firebase-functions/v2/https'
import { getFirestore } from 'firebase-admin/firestore'

// Take the text parameter passed to this HTTP endpoint and insert it into
// Firestore under the path /messages/:documentId/original
const addAmessage = onRequest({region: 'europe-west3'}, async (req, res) =>
{
    // Grab the text parameter.
    const message = req.query.text

    if (message !== undefined) {
        // Push the new message into Firestore using the Firebase Admin SDK.
        const writeResult = await getFirestore()
            .collection('messages')
            .add({ message })
        // Send back a message that we've successfully written the message
        res.json({ result: `Message with ID: ${ writeResult.id } added.` })
    }
})

export default addAmessage

huangapple
  • 本文由 发表于 2023年5月22日 19:55:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定