【发布时间】: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