code-8
code-8

Reputation: 58672

Firebase Database Query Error : child is not a function

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

Upvotes: 3

Views: 4807

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83113

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');

Upvotes: 4

Related Questions