Realtime Database ref change can' t detect - node.js

I created a Db. Name Tomris
Below that is a 'Cumle': "Sentence". Whatever I do here does not happen.
https://i.stack.imgur.com/EEKAk.png
What I want is Trigger work when the sentence changes. But I couldn't.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
exports.DBTrigger = functions.database.ref('tomris-d96a7/Cumle')
.onWrite( (change, context) => {
const afterData = change.after.data();
console.log(afterData);
console.log ("onur");
});

You included the name of your database in the Cloud Functions trigger, which is not needed and in fact means your Function is now looking at a non-existing path.
To trigger on the correct path, only include that path in the Function definition:
exports.DBTrigger = functions.database.ref('Cumle')
...

Related

how to write function for a specific document in firestore?

below is the function, i have used from youtube .
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.androidPushNotification =
functions.firestore.document('MyMoneyNotifications/{MyPercentageMoney}').onCreate(
(snapshot, context)=>
{
admin.messaging().sendToTopic(
"new_user_forums",
{
notification:{
title:snapshot.data().title,
body: snapshot.data().body
}
});});
it is working fine.
but i want to check for below structure.
Notifications(collection)
->UserId(document)
->MyNotification(collection)
->notify1
->notify2
now, i want to check for a specific user if he had any new notifications. how to check for the collection "MyNotification" in firebase functions
If you are trying to listen to sub-collections then you can set the path to:
exports.androidPushNotification =
functions.firestore.document('Notifications/{userId}/MyNotification/{notificationId}')
You can read more about this in the documentation

Firebase functions multi child change

hi firebase functions I want to change multiple children, but cannot change all children under user id. can you help me with this topic
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var database = admin.database();
exports.newNodeDetected = functions.database.ref('system/{userId}/value')
.onWrite((Change) =>{
var oldDeger = Change.before.val();
var newDeger = Change.after.val();
if (newDeger === '0'){
database.ref(`system/${userId}/value`).set('1');
}
})
enter image description here
You can update multiple children in several ways using standard object manipulation and direct writes as demonstrated in your question.
You may be interested in using update rather than set when updating multiple fields.
You will also notice I added a Context field which allows you to access information such as the userID from the reference path and other information: Source
exports.newNodeDetected = functions.database.ref('system/{userId}/value')
.onWrite((Change, Context) =>{
var oldDeger = Change.before.val();
var newDeger = Change.after.val();
if (newDeger === '0'){
database.ref(`system/${Context.params.userId}/value`).set('1');
database.ref(`system/${Context.params.userId}/otherField`).update({another:field});
}
})
Resource: Medium Article: Realtime Set Vs Update

Saving to two different Firestore databases with dialogflow

I'm making an actions on google project that will require adding data to two different Cloud Firestore. For some reason when I trigger the intent, it will only save to the original Cloud Firestore, but not the new one.
For simplicity, I'm going to refer to the original Cloud Firestore as "DB1" and the new one/ second one as "DB2"
Here's what I had tried:
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const admin = require('firebase-admin');
const {google} = require('googleapis');
const {
<Basically All of the libraries / AOG functions>
} = require('actions-on-google');
const defaultAppConfig = {"<FIREBASE CREDENTIALS FOR DB1 >"}
const OtherAppConfig = {"<FIREBASE CREDENTIALS FOR DB2>"}
const defaultApp = admin.initializeApp(defaultAppConfig); // DB1
const otherApp = admin.initializeApp(OtherappConfig, 'Other'); // DB2
const db = admin.firestore(functions.config(defaultApp).firebase); //DB1
const ab = admin.firestore(functions.config(otherApp).firebase); // DB2
const app = dialogflow({
debug: true,
clientId: '<DIALOGFLOW CREDENTIALS>'
});
app.intent('DB2 Write', (conv) =>{
conv.ask('Okay I made the write to DB2');
var data = {
name: 'This is a Test write'
};
var setDoc = ab.collection('Test').doc('Write').set(data);
});
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);
Sorry if some parts are unnecessary, I wanted to include as much information as I could (I might be missing something that someone else sees).
To sum up what I thought would happen, I thought when I triggered the intent 'DB2 Write' that it would write 'This is a Test Write' to DB2, however it just keeps writing the message/data to DB1.
How do I get this working so it will write to my second Cloud Firestore or "DB2" when this intent is triggered?
Thanks for the help!
Note: If it makes a difference, I'm using the dialogflow inline editor for this code.
____________________________________________________________________________
Update: Here is what I have tried/ updated and it still writes to DB1
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
const otherAdmin = require('firebase-admin');
otherAdmin.initializeApp({
credential: otherAdmin.credential.cert(OtherAppConfig)
},'Other');
const ab = otherAdmin.firestore();
and as well:
admin.initializeApp(defaultAppConfig);
var otherApp = admin.initializeApp(OtherAppConfig, 'other');
console.log(admin.app().name); // '[DEFAULT]'
console.log(otherApp.name); // 'other'
// Use the shorthand notation to retrieve the default app's services
var db = admin.firestore(functions.config().firebase);
// Use the otherApp variable to retrieve the other app's services
var ab = otherApp.firestore(functions.config().firebase);
I'd like to note, the credentials I'm using for "OtherAppConfig" and "defaultAppConfig" were taken from the Firebase private key. ie: firebase console > project overview > service accounts > generate private key. Could this be the problem?
I think the problem is thus:
A Dialogflow project and a Firebase project are the same under the hood. This cool, as your Firebase Functions will know intuitively connect with Dialogflow and your database without a lot of manual configuration.
However, if you have two databases from different Cloud Projects, you will need to do some additional configurations to connect securely. I'm not sure what your AppConfigs contain, but they may not be sufficiently setup. As such, the Firebase setup may be pulling the default (current project) app and database when you're grabbing the functions config.
You may want to, for your second project, download the service key. Then you can load it as a file or directly as JSON in your startup routine.
This snippet below should work the way you want.
// Setup 1st db, this project's db
const admin = require('firebase-admin');
admin.initializeApp(); // Initialize with default params as we get it by default
const db = admin.firestore();
// Setup 2nd db
const otherAdmin = require('firebase-admin'); // We can import twice
const myOtherServiceKey = { ... }
otherAdmin.initializeApp({
credential: otherAdmin.credential.cert(myOtherServiceKey)
});
const ab = otherAdmin.firestore(); // This should be the DB of our second project

Accessing Value from Key via Change (onWrite)

I am trying to access the value of a key(node) from the firebase realtime database. I am using the onWrite trigger which passes the change paramters, which holds snapshot of the database before the trigger and after the trigger. Therefore with the after trigger's snapshot I wish to place the key of a node into a constant variable although when I try I recieve a TypeError.
Type Error Bug:
TypeError: change.after.ref.child(...).val is not a function
at exports.sendNotification.functions.database.ref.onWrite (/user_code/index.js:13:64)`
Thus meaning that .val() cannot be used in the way I have tried to use it, although I cannot see any other information to see how to handle this.
Relevant Segment of Code:
'user-strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase); // load the firebase-functions and firebase-admin modules, and initialize an admin app instance from which Realtime Database changes can be made.
exports.sendNotification = functions.database.ref("/Notifications/{user_id}/{notification_type}/{sender_id}").onWrite((change, context) => {
const user_id = context.params.user_id;
const notification_type = context.params.notification_type;
const sender_id = context.params.sender_id;
//Check if the notification is still to be sent (true not false)
const notficationValid = change.after.ref.child(sender_id).val();
console.log(`Notification Valid: `, notficationValid);
I expected the notificationValid variable to hold the value of 'true'.
Firebase Database
The .ref in your code is not needed:
change.after.child(sender_id).val()
Calling .ref on a DataSnapshot gives you back a DatabaseReference, which doesn't have a val() method.

Copy object from one node to another in Cloud Functions for Firebase

I'm using Cloud Functions for Firebase, and I'm stuck with what seems to be a very basic operation.
If someone adds a post, he writes to /posts/. I want a portion of that post to be saved under a different node, called public-posts or private-posts, using the same key as was used in the initial post.
My code looks like this
const functions = require('firebase-functions');
exports.copyPost = functions.database
.ref('/posts/{pushId}')
.onWrite(event => {
const post = event.data.val();
const smallPost = (({ name, descr }) => ({ name, descr }))(post);
if (post.isPublic) {
return functions.database.ref('/public-posts/' + event.params.pushId)
.set(smallPost);
} else {
return functions.database.ref('/private-posts/' + event.params.pushId)
.set(smallPost);
}
})
The error message I get is: functions.database.ref(...).set is not a function.
What am I doing wrong?
If you want to make changes to the database in a database trigger, you either have to use the Admin SDK, or find a reference to the relevant node using the reference you've been given in the event. (You can't use functions.database to find a reference - that's used for registering triggers).
The easiest thing is probably to use event.data.ref (doc) to find a reference to the location you want to write:
const root = event.data.ref.root
const pubPost = root.child('public-posts')

Resources