首页 > 解决方案 > 无法从数据库中获取云功能的任何数据

问题描述

它总是超时并且不打印任何东西

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'));
  })

})

标签: node.jsgoogle-cloud-firestoregoogle-cloud-functions

解决方案


该函数总是超时,因为它从不向客户端发送响应。对于 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();
})

推荐阅读