When I use this function I get the following result from my realtime database. It looks like a json object.
How can I turn that to an array or retrieve the string userName? snapshot.userName is not working.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.Push = functions.database.ref('/placeID/{pushId}/')
.onCreate((snapshot, context) => {
console.log(snapshot)
})
2
Answers
A Realtime Database
DataSnapshot
object is a container that contains your data. To get the data it contains, you need to retrieve its value using theval()
method:To get the
userName
from your data, you can use either:or
See following link for more info about the
DataSnapshot
object and available methods/properties:https://firebase.google.com/docs/reference/functions/providers_database.datasnapshot
Added note: The
DataSnapshot
class overrides thetoJSON()
method, this is why when you log it, you saw the data it contained rather than the DataSnapshot’s own methods/properties.snapshot
is a reference to a Firestore document. In order to get the data contained in a documentdoc
you need to calldoc.data()
. So in your case it would bedoc.data().userName
.See the documentation for some examples.