Faruk
Faruk

Reputation: 853

Querying firebase database at onwrite event

I am trying to write a firebase function which will check the the "user" and behave according to that on database write event. However when i query the database it returns null everytime and i didnt figure out what i am doing wrong. Any help is appreciated.

Thanks in advance.

My realtime database structure is like this:

ilk-projemiz-cd55baddclose
 users
   edRIPg8BcZU9YPbubp7HtQo7phl1
     sayilar: 1532
     status:  "on"
   hakan
     sayilar:  5000
     status:  "waiting"
   mehmet
     sayilar:  7000
     status:  "on"

My firebase function file is this:

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

exports.sayi = functions.database.ref("/users/{uid}/status").onWrite(event => {
    const status = event.data.val();
    var user = event.data.ref.parent.key;
    if (status =="on") {
        console.log(status);
        const events = event.data.adminRef.child('users');
        const query =events.orderByChild('status').equalTo('on').limitToFirst(2);
        query.on("value", sorunsuz,sorunlu);
    }
});

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

function sorunsuz(data) {
    console.log("11111");
    var fruits = data.val();
    console.log(fruits); //it returns null here
    var keys = Object.keys(fruits);
    for (var i = 0; i < keys.length; i++) {
      var key = keys[i];
      if(key==user){
        //console.log(fruits[key].sayilar);
        console.log("aaa");
      }else{
        console.log("bbbb");
      }
    }
}

Upvotes: 0

Views: 532

Answers (2)

Ros&#225;rio P. Fernandes
Ros&#225;rio P. Fernandes

Reputation: 11316

This line: const events = event.data.adminRef.child('users'); tries to access a users node under the status node. And I think what you wanted to do is access the users node under the root reference.

Use the Admin SDK instead:

const events = admin.database().child('users');

Update: the user variable is out of scope, so I suggest you move the sorunsuz() function to be inside the on() function:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sayi = functions.database.ref("/users/{uid}/status").onWrite(event => {
    const status = event.data.val();
    var user = event.data.ref.parent.key;
    if (status =="on") {
        console.log(status);
        const events = admin.database().child('users');
        const query =events.orderByChild('status').equalTo('on').limitToFirst(2);
        query.on("value",
         function(data) {
            console.log("11111");
            var fruits = data.val();
            console.log(fruits); //it returns null here
            var keys = Object.keys(fruits);
            for (var i = 0; i < keys.length; i++) {
                var key = keys[i];
                if(key==user){
                    //console.log(fruits[key].sayilar);
                    console.log("aaa");
                }else{
                    console.log("bbbb");
                }
            }
        }, sorunlu);
    }
});

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

Upvotes: 1

rijin
rijin

Reputation: 1759

you have to listen to users node

functions.database.ref("/users/{uid}/status"), this path is not exist anywhere thats why you are getting null.

exports.sayi = functions.database.ref("/users").onWrite(event => {
   const status = event.data.val(); //this data will be new
   //Use above value to refer things

   if (event.data.previous.exists()) {
     //Update operation
  }

});

Upvotes: 1

Related Questions