lundi 3 avril 2017

Firebase realtime db query in AWS Lambda does not get most up to date result

I am building a web app using firebase realtime db and AWS Lambda + AWS API Gateway for db queries. I am running into an issue where AWS Lambda does not return most up to date items.

For example, if my db has items A, B, C where C is the latest. If I query using the lambda function, it will return A, B, C. If I add D, the lambda "may or may not" return A, B, C, D. It may just return A, B, C. This is super frustrating and I cannot find root cause. I can only assume that it has got something to do with lambda's freezing process implementation. Or maybe it has something to do with the nature of firebase's realtime database where it listens for changes on data? I am not sure.

If someone has encountered similar issue and has found solution, please share the solution.

I will add code snippet of my aws lambda function.

var admin = require("firebase-admin");
var serviceAccount = require("myserviceaccount");

var app = admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: <firebaseDB>
});

exports.handler = (event, context, callback) => {
    context.callbackWaitsForEmptyEventLoop = false;

    var items = [];
    var filter = event.params.querystring.filter;
    admin.database().ref('items').orderByChild('filter').equalTo(filter).limitToLast(20)
    .on('value', function(snapshots) {
        snapshots.forEach(function(snapshot) {
            var item = {
                itemId: snapshot.getKey(),
                data: snapshot.val()
            };
            items.push(item);
        });
        callback(null, feeds);
    });
};

Regards,




Aucun commentaire:

Enregistrer un commentaire