3

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 tried to query it using NodeJS

/*================================
=            Database            =
================================*/

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 db = firebase.database();
var users = db.child('users');
var query = users.orderByChild('fcmToken');

console.log(users);

I kept getting this error :

⚡️  node  node app.js                                                           
/Users/john/Desktop/Apps/APNS/node/app.js:17                                  
var users = db.child('users');                                                 
               ^                                                               

TypeError: db.child is not a function                                          
    at Object.<anonymous> (/Users/john/Desktop/Apps/APNS/node/app.js:17:16)   
    at Module._compile (module.js:643:30)                                      
    at Object.Module._extensions..js (module.js:654:10)                        
    at Module.load (module.js:556:32)                                          
    at tryModuleLoad (module.js:499:12)                                        
    at Function.Module._load (module.js:491:3)                                 
    at Function.Module.runMain (module.js:684:10)                              
    at startup (bootstrap_node.js:187:16)                                      
    at bootstrap_node.js:608:3  

db.child is not a function

I followed the exact syntax from Firebase tutorial:

https://www.youtube.com/watch?v=3WTQZV5-roY

Look at 4:33 minutes you will see it

enter image description here

I hope someone can shed some lights on this

1 Answer 1

4

I don't know where did you find this example (apparently from the YouTube Firebase channel !??) but the child() method is a method of a Reference, not of the Database.

You should therefore do:

var db = firebase.database();
var users = db.ref().child('users');
var query = users.orderByChild('fcmToken');

or directly:

var db = firebase.database();
var users = db.ref('users');
var query = users.orderByChild('fcmToken');
Sign up to request clarification or add additional context in comments.

3 Comments

We should report this error to the Firebase team. Will add a comment below the video.
If I want to see those 2 records in console, how do I do it ? i.imgur.com/ZTNel0N.png
Have a look at the doc of a DataSnapshot firebase.google.com/docs/reference/node/… and use snap.forEach(function(childSnapshot) {})

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.