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)