试图访问实时数据库时,Firebase Node.jspipe理SDK超时

使用带有Firebase函数的Node.js admin SDK每当我尝试访问实时数据库时,都会收到一个超时值。 只有在本地testing函数( firebase serve --only functions,hosting仅适用firebase serve --only functions,hosting )以及使用functions.config().firebase firebase serve --only functions,hosting初始化默认应用程序时才会发生这种情况。

这是一个刚刚发生在几天前的新行为。 但是,如果我尝试使用serviceAccount.json文件初始化默认应用程序,则一切正常。

我正在使用firebase-admin版本4.2.1firebase firebase-functions版本0.5.9

我写了一个简单的http triggered函数,由于超时而失败:

 const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); const db = admin.database(); exports.testDbConnection = functions.https.onRequest((req, res) => { return admin.database().ref().once('value') .then(function(snapshot) { res.json(snapshot); }).catch(function(error) { res.json(error); }); }); 

从文档

始终使用send(),redirect()或end()结束HTTPfunction。 否则,您的function可能会继续运行,并被系统强行终止

请参阅https://firebase.google.com/docs/functions/http-events#terminate_http_functions

这可能取决于您正在使用的firebase-tools版本,但看起来很熟悉这个Github问题

解决scheme是升级到最新版本的CLI或使用解决方法:

  1. 转到https://cloud.google.com/console/iam-admin/serviceaccounts
  2. 点击“创build服务帐户”,给它一个名称(如模拟器),给它的项目>所有者angular色。检查“提供一个新的私钥”,select“JSON”。
  3. 将文件保存在计算机上的某处
  4. 运行export GOOGLE_APPLICATION_CREDENTIALS="absolute/path/to/file.json"
  5. 运行firebase serve --only functions