Wednesday, 31 May 2017

Update data in firebase using nodejs

I have the following problem, I am sending data every minute to a firebase database in a cron tab that is running in nodejs, I only send the information when there are changes, but when there are no changes the database continues to receive information, This is my code

let admin = require('firebase-admin');
let prev_res = {};

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: 'mydburl'
});

cron.schedule('* * * * *', function(){
    let connection = mysql.createConnection({
      host     : 'myhost',
      user     : 'myuser',
      password : 'mypass',
      database : 'mydb'
    });

    connection.query("MY QUERY", function(err, rows, fields){
        if (!err){
            if(JSON.stringify(rows) != JSON.stringify(prev_res)){
                let db = admin.database();
                let ref = db.ref('path');
                ref.set(rows);

                console.log("Updated data");
            } else {
                console.log("without changes");
            }

            prev_res = rows;

        }
    });

});

Does the firebase admin have some kind of cache or something like that?



via Mauricio Ramos

No comments:

Post a Comment