how to update a value in firebase realtime database using Cloud Functions for Firebase

Thooyavan Manivaasakar

I went though the firebase docs for updating a value in realtime database using Cloud Functions for Firebase, but am not able to understand.

My database structure is

{   
 "user" : {
    "-KdD1f0ecmVXHZ3H3abZ" : {
      "email" : "[email protected]",
      "first_name" : "John",
      "last_name" : "Smith",
      "isVerified" : false
    },
    "-KdG4iHEYjInv7ljBhgG" : {
      "email" : "[email protected]",
      "first_name" : "Max1",
      "last_name" : "Rosse13131313l",
      "isVerified" : false
    },
    "-KdGAZ8Ws6weXWo0essF" : {
      "email" : "[email protected]",
      "first_name" : "Max1",
      "last_name" : "Rosse13131313l",
      "isVerified" : false
    } 
}

I want to update the isVerified using database trigger cloud functions. I don't know how to update a database value using cloud functions (language : Node.JS)

I wrote a code to automatically update the value of the key 'isVerified' of a user, when the user is created by using database trigger onWrite. My code is

const functions = require('firebase-functions');

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

exports.userVerification = functions.database.ref('/users/{pushId}')
    .onWrite(event => {
    // Grab the current value of what was written to the Realtime Database.
    var eventSnapshot = event.data;

    if (event.data.previous.exists()) {
        return;
    }

    eventSnapshot.update({
        "isVerified": true
    });
});

but when i deploy the code, and add a user to the database, the cloud function log shows the below error

TypeError: eventSnapshot.child(...).update is not a function
    at exports.userVerification.functions.database.ref.onWrite.event (/user_code/index.js:10:36)
    at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:35:20
    at process._tickDomainCallback (internal/process/next_tick.js:129:7)
Doug Stevenson

You are trying to call update() on a DeltaSnapshot object. There is no such method on that type of object.

var eventSnapshot = event.data;
eventSnapshot.update({
    "isVerified": true
});

event.data is a DeltaSnapshot. If you want to change the data at the location of the change represented by this object. Use its ref property to get a hold of a Reference object:

var ref = event.data.ref;
ref.update({
    "isVerified": true
});

Also, if you are reading or writing the database in a function, you should always return a Promise that indicates when the change is complete:

return ref.update({
    "isVerified": true
});

I would recommend taking Frank's advice from the comments and study the existing sample code and documentation to better understand how Cloud Functions works.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Android

How to create a new parent node outside the .ref(/path) in Firebase Realtime Database using Cloud Functions (Typescript)?

From Java

onClick int Value update to Firebase Realtime Database

From Dev

How to access multiple Realtime Database instances in Cloud Functions for Firebase

From Dev

Can't update firebase database using firebase cloud functions without using database triggers

From Dev

Firebase Cloud Functions Shell not executing write operations on realtime database

From Dev

How to update value of a particular child node in firebase realtime database?

From Dev

How to use scheduler for Firebase Cloud Functions with Realtime Database/Analytics triggers?

From Dev

Dynamically assign a key value when writing a new data to firebase realtime database using firebase cloud functions

From Dev

Firebase Realtime Database and Cloud Functions -- increment counter based on data nodes

From Dev

Firebase Cloud Functions: Cannot pass the token retrieved from Realtime Database

From Dev

How to get Realtime Database in Firebase by using cloud function?

From Dev

Set all instances of a value to null in Realtime Database: Firebase Cloud Functions

From Dev

How do I use Firebase Admin SDK to change data in Firebase Realtime Database while using Cloud Functions?

From Dev

How to update an existing value in the Firebase Realtime Database?

From Dev

HOW TO Update where in firebase realtime database?

From Dev

update database firebase realtime

From Dev

Firebase Cloud Functions - Update a value using onWrite event

From Dev

How to trigger realtime database field in Google Cloud server (not in functions of Firebase)

From Dev

How to push new data into firebase realtime database using cloud functions?

From Dev

Firebase cloud function with realtime database triggers: how to update the source node?

From Dev

How to fetch data from firebase-realtime-database using firebase cloud functions?

From Dev

How to update firebase realtime database based on a value in array of objects in flutter?

From Dev

Update single value in Firebase Realtime Database using Rest API

From Dev

How to create new Firebase Realtime Database Instance using Cloud Functions?

From Dev

Firebase Cloud Functions with TypeScript: Realtime Database update ends with success but not updates anything, JS works fine

From Dev

How to update value directly in firebase realtime database inside dynamic key

From Dev

Firebase Realtime Database Triggers on Cloud Run or Kubernetes, Not Cloud Functions

From Dev

How to listen to realtime database changes (like a stream) in firebase cloud functions?

From Dev

How to get a specific value from firebase realtime database using kotlin

Related Related

  1. 1

    How to create a new parent node outside the .ref(/path) in Firebase Realtime Database using Cloud Functions (Typescript)?

  2. 2

    onClick int Value update to Firebase Realtime Database

  3. 3

    How to access multiple Realtime Database instances in Cloud Functions for Firebase

  4. 4

    Can't update firebase database using firebase cloud functions without using database triggers

  5. 5

    Firebase Cloud Functions Shell not executing write operations on realtime database

  6. 6

    How to update value of a particular child node in firebase realtime database?

  7. 7

    How to use scheduler for Firebase Cloud Functions with Realtime Database/Analytics triggers?

  8. 8

    Dynamically assign a key value when writing a new data to firebase realtime database using firebase cloud functions

  9. 9

    Firebase Realtime Database and Cloud Functions -- increment counter based on data nodes

  10. 10

    Firebase Cloud Functions: Cannot pass the token retrieved from Realtime Database

  11. 11

    How to get Realtime Database in Firebase by using cloud function?

  12. 12

    Set all instances of a value to null in Realtime Database: Firebase Cloud Functions

  13. 13

    How do I use Firebase Admin SDK to change data in Firebase Realtime Database while using Cloud Functions?

  14. 14

    How to update an existing value in the Firebase Realtime Database?

  15. 15

    HOW TO Update where in firebase realtime database?

  16. 16

    update database firebase realtime

  17. 17

    Firebase Cloud Functions - Update a value using onWrite event

  18. 18

    How to trigger realtime database field in Google Cloud server (not in functions of Firebase)

  19. 19

    How to push new data into firebase realtime database using cloud functions?

  20. 20

    Firebase cloud function with realtime database triggers: how to update the source node?

  21. 21

    How to fetch data from firebase-realtime-database using firebase cloud functions?

  22. 22

    How to update firebase realtime database based on a value in array of objects in flutter?

  23. 23

    Update single value in Firebase Realtime Database using Rest API

  24. 24

    How to create new Firebase Realtime Database Instance using Cloud Functions?

  25. 25

    Firebase Cloud Functions with TypeScript: Realtime Database update ends with success but not updates anything, JS works fine

  26. 26

    How to update value directly in firebase realtime database inside dynamic key

  27. 27

    Firebase Realtime Database Triggers on Cloud Run or Kubernetes, Not Cloud Functions

  28. 28

    How to listen to realtime database changes (like a stream) in firebase cloud functions?

  29. 29

    How to get a specific value from firebase realtime database using kotlin

HotTag

Archive