从PubSub触发的云功能写入Firebase数据库 [英] Write to firebase database from cloud function triggered by PubSub

查看:125
本文介绍了从PubSub触发的云功能写入Firebase数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用每日触发器来更改Firebase数据库中的信息.我还有许多其他可以正常运行的云功能,但是我似乎无法使用此功能来更改数据库.

I'm trying to use a daily trigger to alter information in my Firebase database. I have a number of other cloud functions that work properly, but I can't seem to get this function to alter the database.

const admin = require('firebase-admin');
exports.daily_job = functions.pubsub.topic('daily-tick').onPublish((event) => {
  console.log("This job is run every day!");
  const databaseRef = admin.database().ref('/accountActions/dailyDeletion');
  databaseRef.child('delete').set("Data has been deleted!");
  return "End of database clearing";
});

这是我一直在测试的代码,以查看触发器是否正常工作.在我的控制台中,该函数启动,记录了控制台语句,并且执行在40-90毫秒内完成.但是我的数据库从未改变.删除语句永远不会出现.

This is the code I've been testing to see if the trigger is working. In my console it's showing that the function starts, the console statement is logged, and execution finishes in 40-90ms. But my database is never changed. The deletion statement never appears.

任何帮助将不胜感激!

Any help is greatly appreciated!

推荐答案

我找到了问题的答案.我已经根据Firebase的推送通知文档初始化了该应用程序:

I found the answer to the problem. I had initialized the app according to Firebase's documentation on push notifications:

admin.initializeApp({


  credential: admin.credential.cert({
    projectId: {projectId},
    clientEmail: {email},
    privateKey: {private key}
  }),
 databaseURL: {databaseURL}
 });

但是由于某种原因,这导致了问题.我用一个简单的

But for some reason this caused issues. I switched it out with a simple

admin.initializeApp(functions.config().firebase);

这似乎已经解决了该问题,并且推送通知仍然有效.

This seems to have fixed the issue, and push notifications are still working.

这篇关于从PubSub触发的云功能写入Firebase数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆