0

I have a table users in Firebase with these data

{
  "users" :
  [
   {"id": 1,
     "fcmToken" : "APA91bHJAzXe384OEYvfk4bKsyS1NQvteph7DwG7JRIMm_HuXg8EeNllVrsSi0v9W_Gh95ezbOStp3ZWuWl0AzFKxMaCOjN81yiz7A5qhkONrd7lP2CTkUbFErw28r3ONTLvo8c8sO7hdiWY78iar8s:APA91bHJAzXe384OEYvfk4bKsyS1NQvteph7DwG7JRIMm_HuXg8EeNllVrsSi0v9W_Gh95ezbOStp3ZWuWl0AzFKxMaCOjN81yiz7A5qhkONrd7lP2CTkUbFErw28r3ONTLvo8c8sO7h",
     "fName" : "John",
     "lName" : "Doe",
     "phone" : "9786770861"
   },
   {"id": 2,
     "fcmToken" : "APA91bHJAzXe384OEYvfk4bKsyS1NQvteph7DwG7JRIMm_HuXg8EeNllVrsSi0v9W_Gh95ezbOStp3ZWuWl0AzFKxMaCOjN81yiz7A5qhkONrd7lP2CTkUbFErw28r3ONTLvo8c8sO7hdiWY78iar8s:APA91bHJAzXe384OEYvfk4bKsyS1NQvteph7DwG7JRIMm_HuXg8EeNllVrsSi0v9W_Gh95ezbOStp3ZWuWl0AzFKxMaCOjN81yiz7A5qhkONrd7lP2CTkUbFErw28r3ONTLvo8c8sO7h",
     "fName" : "Jane",
     "lName" : "Doe",
     "phone" : "6178779690"
   }

 ]
}

after import I get this

enter image description here

I followed this exact post from Firebase

https://firebase.google.com/docs/reference/node/firebase.database.DataSnapshot

enter image description here

var admin = require("firebase-admin");

var serviceAccount = require('/Users/john/Desktop/Apps/APNS/node/mhn-app-firebase-adminsdk-bs45c-5ac3770488.json');

var firebase = admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://mhn-app.firebaseio.com"
});


var ref = firebase.database().ref("users");
ref.once("value")
.then(function(snapshot) {

    var users =  snapshot.child("users").val();
    console.log(users);

});

I kept getting null

⚡️  node  node app.js                                                           
>>> Done                                                                       
null                                                                           



^C                                                                             
⚡️  node 

1 Answer 1

2

You're querying to get the /users node, and then ask for the users child in there. There is no /users/users, so you get an empty result.

The solution is:

ref.once("value").then(function(snapshot) {
    var users = snapshot.val();
    console.log(users);
});

You'll note that in the example you followed, they're also not doing snapshot.child('users/ada') in the callback.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.