0

I'm trying to add some triggered functions to my firebase account but before that i wanted to learn the basics of firebase queries so i wrote a js file and testing it by writing "node index.js" in my command prompt. Learnt a few basic queries from youtube etc and tried to integrate it in my js file however so far no luck at all. All my queries returns null and i didnt understand what part of it i am doing. Could you please have a look.

Thanks in advance.

My database structure

<firebasename>
 users
   edRIPg8BcZU9YPbubp7HtQo7phl1
     sayilar: 2653
     status: "ok"
   hakan
     sayilar: 5000
     status: "waiting"
   mehmet
     sayilar: 7000
     status: "ok"

My database rule:

{
  "rules": {
    "users": {
        ".read": true,
        ".write": true
    }
  }
}

My index.js file:

var firebase = require('firebase');

var config = {
  apiKey: "<my-api-key>",
  authDomain: "<firebasename>.firebaseio.com",
  databaseURL: "https://<firebasename>.firebaseio.com",

};
firebase.initializeApp(config);
ref = firebase.database().ref();
const events = ref.child('users').child('hakan');
const query =events.orderByChild('status').equalTo('ok').limitToFirst(2);

query.on("value", sorunsuz,sorunlu);

function sorunlu(error) {
  console.log("Something went wrong.");
  console.log(error);
}

function sorunsuz(data) {
  
 
    var fruits = data.val();
    console.log(fruits);
}

The command promt shows

null

FIREBASE WARNING: Using an unspecified index. Your data will be downloaded and filtered on the client. Consider adding ".indexOn": "status" at /users/hakan to your security rules for better performance.

1 Answer 1

3

The issue with the code is here:

ref = firebase.database().ref();
const events = ref.child('users').child('hakan');

events targets this part of the data:

 users
   hakan
     sayilar: 5000
     status: "waiting"

Then you call

const query =events.orderByChild('status').equalTo('ok').limitToFirst(2);

Which queries the children of hakan by their children status, which will return null because neither of the keys under hakan have a child status. It seems like you want to change events to:

const events = ref.child('users')

Then you would order the children of users by their child status.

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

1 Comment

You'll also want to add an index under /users/$userid to get rid of that warning (and prevent the SDK from downloading all user data and filter on the client).

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.