我一直在使用Firebase云函数,并且遇到了这样的问题:我的函数有时在返回数据之前会挂起长达10秒钟。我看了看文档,并说一旦完成,我应该随数据一起返回一个承诺。我的结构目前是这样的:
export const SomeFnName = functions.https.onCall(async (params: SomeTypedParams, context: functions.https.CallableContext) =>
{
// Bunch of validation that throw functions.https.HttpsError if there is one
// load some data from the server
const snapshot = await admin.firestore().collection(Collections.SOMEKEY).doc(params.SOMEID).get();
// The the snapshot data
const someData: FirebaseFirestore.DocumentData | undefined = snapshot.data();
// Do some processing of above then return
return {
data1: someData["key1"],
data2: someData["key2"]
};
}
如您所见,我正在从一个单独的文件中导出函数,然后像这样注册
exports.SomeFnName = SomeFnName;
这是返回经过处理的数据的正确方法吗?如果不是,需要做些什么才能使其起作用?我看不到服务器调用有时需要很长时间才能执行,有时甚至会完全失败,我还会做错什么。
答案 0 :(得分:1)
您遇到的情况通常称为“冷启动”。第一次在由无服务器后端分配的新服务器实例上执行功能(例如云功能)时,就会发生这种情况。您没有做错任何事,只是在支付冷启动成本(加上计算机,Cloud Functions和Cloud Firestore之间的任何网络延迟)。