简体   繁体   English

Firebase 云 Function 未触发 - 使用 onCreate - Google Cloud Console

[英]Firebase Cloud Function not trigging - using onCreate - Google Cloud Console

I have searched multiple answers on stackoverflow but cannot seem to find one to solve my problem.我在 stackoverflow 上搜索了多个答案,但似乎找不到一个可以解决我的问题的答案。 I am trying to get my cloud function to run when I create certain new data in Firebase Realtime Database.当我在 Firebase 实时数据库中创建某些新数据时,我试图让我的云 function 运行。

数据库截图

The function deploys fine and shows no errors, but the problem is that when I add data (like the data in the attachment), the function isn't triggered? function 部署正常,没有显示错误,但问题是当我添加数据(如附件中的数据)时,没有触发 function? The data is created new, not updated.数据是新创建的,而不是更新的。

Here is the code function:这是代码 function:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.pushNotificationFromAdmin = functions.database.ref('pushNotifications/{createdID}').onCreate((snapshot, context) => {
  const data = snapshot.val();
  const topic = data.pushGroup.toString();
  const title = data.pushTitle;
  const body = data.pushBody;

  const payload = {
          notification : {
              title: title,
              body: body,
              mutable_content: 'true',
          },
  };

  const options = {
            priority : 'high',
            timeToLive : 60 * 60 * 2
  };
  console.log('Payload is : ' + payload)
  console.log('Sending to Topic ' + topic)
  return admin.messaging().sendToTopic(topic, payload, options);

});

What am I missing?我错过了什么?

Thanks谢谢

You can refer to the [Documentation] where mentioned as:您可以参考提到的[文档]:

When handling a Realtime Database event, the data object returned is a DataSnapshot .处理实时数据库事件时,返回的数据 object 是DataSnapshot For onWrite or onUpdate events, the first parameter is a Change object that contains two snapshots that represent the data state before and after the triggering event.对于 onWrite 或 onUpdate 事件,第一个参数是 Change object,其中包含两个快照,表示触发事件之前和之后的数据 state。 For onCreate and onDelete events, the data object returned is a snapshot of the data created or deleted.对于 onCreate 和 onDelete 事件,object 返回的数据是创建或删除数据的快照。

In this example, the function retrieves the snapshot for the specified path as snap, converts the string at that location to uppercase, and writes that modified string to the database:在此示例中,function 检索指定路径的快照作为 snap,将该位置的字符串转换为大写,并将修改后的字符串写入数据库:

 // Listens for new messages added to /messages/:pushId/original and creates an // uppercase version of the message to /messages/:pushId/uppercase exports.makeUppercase = functions.database.ref('/messages/{pushId}/original').onCreate((snapshot, context) => { // Grab the current value of what was written to the Realtime Database. const original = snapshot.val(); functions.logger.log('Uppercasing', context.params.pushId, original); const uppercase = original.toUpperCase(); // You must return a Promise when performing asynchronous tasks inside a Functions such as // writing to the Firebase Realtime Database. // Setting an "uppercase" sibling in the Realtime Database returns a Promise. return snapshot.ref.parent.child('uppercase').set(uppercase); });

If you want to get more idea how to use triggers effectively for a realtime database, you can refer to this answer and this blog .如果您想更多地了解如何为实时数据库有效地使用触发器,您可以参考这个答案和这个博客

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用控制台触发 Google Cloud 功能 - Triggering Google Cloud function using console Firebase:如何在 onCreate 中获取附加用户信息 Firebase 云 function - Firebase: how to get getAdditionalUserInfo in onCreate Firebase Cloud function 如何将 Google Cloud 凭据导入 Firebase Cloud Function? - How to import Google Cloud credentials into a Firebase Cloud Function? 谷歌云功能 Firebase 使用 NodeJS 更新 FieldValue.increment(1) - TypeError: FieldValue.increment is not a function - Google cloud functions Firebase Update FieldValue.increment(1) using NodeJS - TypeError: FieldValue.increment is not a function 如何使用 Cloud Function 服务帐户在 Cloud Function 中创建签名的 Google Cloud Storage URL? - How to create signed Google Cloud Storage URLs in Cloud Function using Cloud Function service account? Firebase 可调用云 Function CORS 错误使用 Z035489FF8D092741943E4A83241F5 - Firebase Callable Cloud Function CORS Error using Firebase Emulator and Vite 从 Java 中的云函数访问 Google Firebase 数据库中的数据 - Access data in Google Firebase database from a cloud function in Java 使用 SSL 与 Google Cloud/Firebase 通信 IoT 设备 - Communicating IoT Device with Google CLoud/Firebase using SSL 用于文件上传的 Firebase 云功能 - Firebase cloud function for file upload Firebase Cloud Function 中的本地化错误 - Wrong localisation in Firebase Cloud Function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM