[英]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.