0

I have created one small asp.net MVC web application for study purpose which stores data in both Firebase and SQL server. Whenever data is updated in Firebase, I am updating data into SQL server and then calling data from SQL server and after that I am displaying that data into web page.

below is code for updating data in SQL server after updating Firebase.(I am updating 'status' field only)

firebase.database().ref("Table/").on('value',
function (snapshot) {
    snapshot.forEach(function (child) {

        firebase.database().ref("Table/" + child.key + "/").on('value',
            function (snap1) {
                snap1.forEach(function (child1) {

                    firebase.database().ref("Table/" + child.key + "/" + child1.key + "/").on('child_changed', function (s) {
                        var Status, tableId;
                        s.forEach(function (c) {
                            if (c.key == "status") {
                                Status = c.val();
                            }
                            if (c.key == "tableId") {
                                tableId = c.val();
                            }
                        });
                        $.ajax({
                            type: "post",
                            url: "/BranchManager/UpdateStatus",
                            data: { status: Status, id: tableId },
                            datatype: "json",
                            traditional: true,
                            success: function () {
                                console.log("Table Updated: Status");
                            }
                        });
                    });
                });
            });
    });
});

The code is working when application is running in browser.

Here I have written above code in Index.cshtml. So if any data is updated from firebase and application is offile, then after staring application it will load above code and updates data in sql server. But is is not working.

I have referred this document from google but I am unable to get solution.

I am confused when 'child_changed' event is called? Is there any way to get only updated data from Firebase?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Techno Crave
  • 423
  • 1
  • 7
  • 17
  • 1
    If you reload the Index page, all data will be considered new since Firebase has no knowledge of what data you've already stored. For a few similar questions have a look here: https://www.google.com/search?q=site%3Astackoverflow.com+firebase+get+only+new+data. A common approach is to add a timestamp as David shows here: https://stackoverflow.com/a/33885787, or by "remembering" the key of the most recent item you saw as I show here: https://stackoverflow.com/a/34865608/209103 – Frank van Puffelen Apr 03 '18 at 14:34
  • @FrankvanPuffelen Thank you for your suggestions..I have found useful documents to achieve above task.. – Techno Crave Apr 04 '18 at 05:11

0 Answers0