Thursday, 11 May 2017

How to get Firebase admin.database values in Node.js?

I am trying to retrieve a values from Firebase and set them as variables. Based on the docs (https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot) it seems that you can reference a node and use the .val() function to retrieve it.

However, when I try to run this script in Node I get unexpected return values, namely, somethingelse gets logged as null. I can see that a value is present from the Firebase Database dashboard.

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

let ref = admin.database().ref('user/{userId}');
ref.once('value')
  .then(function(snapshot) {
    let something = snapshot.child('name').key;      
    let somethingelse = snapshot.child('name').val();
    console.log(something);                // logs: name
    console.log(somethingelse);            // logs: null
  });

Is the value function available in the admin reference, or is just for firebase references? I am trying to run as a Cloud Function for Firebase...



via KVNA

No comments:

Post a Comment