javascript - Firestore 需要一分钟多的时间才能在 Firebase Cloud Function 中获取文档
问题描述
我正在尝试通过在 Firebase 的触发云功能中使用 firebase-admin 来获取内部文档:
exports.onTransactionCreated = functions.firestore
.document("transaction/{id}")
.onCreate((snapshot, context) => {
const first = Date.now();
admin.firestore().collection('myCollection').doc(snapshot.data().myDocumentId).get()
.then((documentSnapshot) => {
const second = Date.now();
functions.logger.log(`seconds total = ${Math.floor((third - first) / 1000)}`);
}
}
控制台日志显示此结果:
seconds bw 1-2 elapsed = 140
使用的版本:
"engines": {
"node": "12"
},
"dependencies": {
"firebase-admin": "^9.2.0",
"firebase-functions": "^3.11.0"
}
在什么情况下可以检索到那么长的文档?即使在冷启动的情况下,我也不敢相信它会那么长。这个问题实际上是我的应用程序的一个大痛点,任何帮助将不胜感激。
解决方案
您的函数需要返回一个在所有异步工作完成后解析的承诺。现在,您的函数什么也不返回,这意味着它将终止而无需等待任何东西。
最低限度,您应该返回由get().then(...)
.
exports.onTransactionCreated = functions.firestore
.document("transaction/{id}")
.onCreate((snapshot, context) => {
const first = Date.now();
return admin.firestore().collection('myCollection').doc(snapshot.data().myDocumentId).get()
.then((documentSnapshot) => {
const second = Date.now();
functions.logger.log(`seconds total = ${Math.floor((third - first) / 1000)}`);
}
}
请参阅文档以获取更多信息。正确处理 Promise 是使函数正常工作的关键。
推荐阅读
- blazor - 在 Blazor 中的文本或数字框中自动突出显示
- flutter - 如何使用borderRadius向容器添加高程
- vb.net - 在运行时使用继承本地化用户控件 VB.net
- python - 文件列表按字母顺序排序python
- android - 如何制作自定义材质切换按钮?
- mongodb - ubuntu 容器中的 MongoDB 连接失败
- react-native-ios - Xcode 13 更新后出现未定义符号错误
- python - Django:过滤来自前端的日志
- android - React Native Android 应用的生产构建无法启动
- r - 更改默认 ggsave 背景颜色