Sim*_*ård 8 node.js firebase google-cloud-functions
我正在设置一个 Firebase 项目,其功能组织在单独的文件中。我正在用第二代功能尝试这个。
查看文档,似乎尚未更新第二代,因为示例代码函数是第一代: https: //firebase.google.com/docs/functions/organize-functions ?gen=2nd#write_functions_in_multiple_files
即便如此,使用这个简单的设置一切都工作正常:
索引.ts:
import { initializeApp } from 'firebase-admin/app'
import addAmessage from './api/addAmessage'
import app from './onCall/app'
initializeApp()
exports.addAmessage = addAmessage
exports.app = app
Run Code Online (Sandbox Code Playgroud)
在 addAmessage.ts 文件中,我仅使用 Firebase 示例函数添加 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
Run Code Online (Sandbox Code Playgroud)
仅使用一个函数就可以正常工作,但由于我在其他函数文件中设置了使用 setGlobalOptions ,所以我也在模拟器控制台中收到警告:
“调用 setGlobalOptions 两次会导致未定义的行为”
我最初将 setGlobalOptions 方法放在我的 index.ts 文件中,但后来它被忽略了。
所以,我希望只需要 setGlobalOptions 一次,但是如何正确地处理多个文件呢?
小智 12
我刚刚遇到了同样的问题,并且能够通过更改导出来解决它,使您的代码如下所示:
索引.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'
Run Code Online (Sandbox Code Playgroud)
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.` })
}
})
Run Code Online (Sandbox Code Playgroud)
如果需要,您仍然可以通过将选项添加到 onRequest 来覆盖全局选项或将其附加到函数中。
export const addAmessage = onRequest({ minInstances: 1 }, async (req, res) => {
Run Code Online (Sandbox Code Playgroud)
至于为什么会这样,我不知道,也许有人可以进一步解释。
小智 2
我遇到了类似的问题,如果有人仍在为此苦苦挣扎,您可以将选项对象传递给可调用函数。选项之一是region,您可以根据需要进行设置。
这是应该起作用的代码:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1931 次 |
| 最近记录: |