【问题标题】:How to export a typescript Google Cloud Function from separate file?如何从单独的文件中导出打字稿谷歌云函数?
【发布时间】:2021-09-14 01:43:17
【问题描述】:

结构

functions
|--src
    |--index.ts
    |--Admin
        |--querys.ts
        |--migrations.ts

目标

我编写了一个 Google Cloud 函数,它使用 Admin SDK 来查询我的 Firestore 数据库。这个函数写在querys.ts文件中但是需要从index.ts导出 这是我现在拥有的:

index.ts

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import { query } from './Admin/querys';
export const readData = functions
  .https(query);
querys.ts

import * as admin from 'firebase-admin';
import * as fs from 'fs';

const db = admin.firestore();

export async function query() {
  const dataRef = db.collection('Collection')
  const res = await dataRef.get()
  if (res.size == 0) {
    return console.log("The query is empty")
  } else {
    res.forEach(doc => {
      const data = doc.data().consolidatedBucketId
      fs.writeFile('QueryOutput.txt', `${data} \n`, { flag: "a+" }, (err) => {
        console.log('There was an error writing to the file: ', err);
      })
      return res
    })
  }
}

.https(query) 的调用抛出错误:

This expression is not callable.
  Type '{ onRequest: (handler: (req: Request, resp: Response<any>) => void | Promise<void>) => HttpsFunction; onCall: (handler: (data: any, context: CallableContext) => any) => TriggerAnnotated & ... 1 more ... & Runnable<...>; }' has no call signatures.

我正在尝试遵循本教程:Can I write my Cloud Functions in separate file? 但是,他们使用的示例不是调用的 https,我无法编写所需的解决方案。

假设:
看来我没有正确返回 async 函数的承诺?

【问题讨论】:

  • 我建议首先将它们全部写在同一个文件中,确保它有效,然后在另一个文件中拆分你想要的部分。就目前而言,您的函数通常似乎缺少很多代码。我什至不确定它应该做什么。如果您在非工作函数旁边显示一个工作函数以查看您做错了什么,您将更有可能获得帮助。

标签: typescript firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

在单独的文件中编写整个函数似乎可以解决问题:

index.ts:

import {queryFunc} from './Admin/querys'

export const query = queryFunc

query.ts:

import * as functions from 'firebase-functions'

export const queryFunc = functions.https.onRequest((req, res) => {
  const dataRef = db.collection('Collection')
  const dataSnapshot = await dataRef.get()
  if (dataSnapshot.size == 0) {
    return console.log("The query is empty")
  } else {
    dataSnapshot.forEach(doc => {
      const data = doc.data().consolidatedBucketId
      fs.writeFile('QueryOutput.txt', `${data} \n`, { flag: "a+" }, (err) => {
        console.log('There was an error writing to the file: ', err);
      })
    })
    return res.send(dataSnapshot)
    // ^ Send the response back
  }
});

// I've renamed res (from Firestore) to dataSnapshot to avoid confusion

【讨论】:

  • tslint 没有抛出任何错误。但是,当我尝试部署该功能时,我收到:Error: functions predeploy error: Command terminated with non-zero exit code2
  • @Garrett 你能测试在本地模拟器上运行这些功能吗? res.send() 也应该不在 forEach 循环中。请分享错误的完整屏幕截图。
  • 截图:imgur.com/a/ihJdUPk。我查看了错误的错误,不习惯在部署时看到其他文件,抱歉。在将返回移出循环并尝试附加链接后,还有其他问题。这个问题开始失去焦点。您的帮助很棒,但它可能不适合堆栈溢出线程。我可能会采纳道格的建议,并在明天/以后提出一个更有针对性的问题。 garrettohara2018@gmail 如果你想聊天stackoverflow.com/questions/50300271/…
  • @Garrett 他们似乎是 ES Lint 错误......但如果它在模拟器上工作并且您可以使用其他文件中的功能,您可以在确认后接受答案。当你有 Doug 后面提到的 MVP 时,你可以@我
猜你喜欢
  • 1970-01-01
  • 2019-10-15
  • 1970-01-01
  • 2019-06-04
  • 2020-07-28
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 2020-11-23
相关资源
最近更新 更多