How to get database value not related to event in cloud functions for Firebase?

I have a firebase database, and I'm currently trying to use cloud functions to perform an operation when a value changes in my database. So far, it has successfully run the code to run when the value in my database changes. However, when the database value changes, now I need to check another value to determine its status, and then perform an action after that. The problem is that I have experience with JS ~ 0, and I have no way to debug my code other than deploying, changing the value in my database, and viewing the console log.

Is there a way to find another value in the database and read it? How to find a value and then set a value for it? Here is the code:

export.determineCompletion =

functions.database.ref('/Jobs/{pushId}/client_job_complete')
    .onWrite(event => {

        const status = event.data.val();
        const other = functions.database.ref('/Jobs/' + event.params.pushId + '/other_job_complete');
        console.log('Status', status, other);

        if(status == true && **other.getValueSomehow** == true) {
            return **setAnotherValue**;
        }


    });

This code partially works, it successfully receives the value associated with client_job_complete and saves it in status. But how do I get a different value?

Also, if anyone has any JS or firebase documentation that they think will help me, please share! I read a bunch on firebase here: https://firebase.google.com/docs/functions/database-events , but it only talks about events and very briefly

Thank you for your help!

+4
source share
3 answers

, :

event.data.ref
event.data.adminRef

ref , . adminRef .

Reference root, . once().

SDK Firebase admin.

, , , .

+6

, , , :

exports.processJob = functions.database.ref('/Jobs/{pushId}/client_job_complete').onWrite(event => {

    const status = event.data.val();

    return admin.database().ref('Jobs/' + event.params.pushId + '/other_job_complete').once('value').then((snap) => {

        const other = snap.val();
        console.log('Status', status, other);

        /** do something with your data here, for example increase its value by 5 */
        other = (other + 5);

        /** when finished with processing your data, return the value to the {{ admin.database().ref(); }} request */
        return snap.ref.set(other).catch((error) => {
            return console.error(error);
        }); 
    });
});

firebase.

Jobs/pushId/other_job_complete, , uid.

:

const functions         = require('firebase-functions');
const admin             = require('firebase-admin');
const adminCredentials  = require('path/to/admin/credentials.json');

admin.initializeApp({
    credential: admin.credential.cert(adminCredentials),
    databaseURL: "https://your-database-url-com",
    databaseAuthVariableOverride: {
        uid: 'super-special-unique-firebase-admin-uid'
    }
});

firebase :

"client_job_complete": {
    ".read": "auth !== null",
    ".write": "auth.uid === 'super-special-unique-firebase-admin-uid'"
}

, !

+1

once() ref, - :

exports.processJob = functions.database.ref('/Jobs/{pushId}/client_job_complete')
  .onWrite(event => {

    const status = event.data.val();
    const ref = event.data.adminRef.root.child('Jobs/'+event.params.pushId+'/other_job_complete');
    ref.once('value').then(function(snap){
      const other = snap.val();
      console.log('Status', status, other);
      if(status && other) {
        return other;
      }
    });
  });

, , @Doug Stevenson ( "- " )

-1

Source: https://habr.com/ru/post/1673633/


All Articles