node.js - 无法从数据库中获取云功能的任何数据
问题描述
它总是超时并且不打印任何东西
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendFollowerNotification = functions.https.onRequest(async (change, context) => {
// If un-follow we exit the function.
// Get the list of device notification tokens.
const fcm = admin.firestore().collection('fcm');
const snapshot = await fcm.get();
snapshot.forEach(doc => {
console.log(doc.id, '=>', doc.get('uid'));
})
})
解决方案
该函数总是超时,因为它从不向客户端发送响应。对于 HTTP 类型的函数,您需要在所有异步工作完成后发送响应才能正确终止函数,如文档中所述。
您的函数还有另一个问题,即参数名称与正常名称不同。您有change, context
,但这仅适用于数据库功能。由于您有一个 HTTP 函数,因此通常会调用参数req, res
,如文档中所示。
最低限度,您可以简单地调用res.end()
.
exports.sendFollowerNotification = functions.https.onRequest(async (req, res) => {
const fcm = admin.firestore().collection('fcm');
const snapshot = await fcm.get();
snapshot.forEach(doc => {
console.log(doc.id, '=>', doc.get('uid'));
})
res.end();
})
推荐阅读
- kubernetes - 在 Kubernetes YAML 中引用资源属性
- java - Hibernate 和 MYSQL:您的 SQL 语法有错误?
- graphql - 如何在空手道中将数据注入graphql文件
- c# - 列表到 DataGrid 的 WPF DataBinding - 不更新数据网格
- git - Git 存储库停止同步 Azure 本地,而是使用 Visual Studio 中的 Azure 云,需要移回本地
- tensorflow - 无法导入 TensorFlow 2.3
- python - Odoo 12. 为什么使用 xpath 会出现错误?
- java - 在不使用 JAVA 循环的情况下获取列表中包含“.txt”的索引
- reactjs - 如何运行 formatjs 编译?
- makefile - 在 autoconf 中,如何创建临时文件和测试 shell 命令?