How to only get new data without existing data from a Firebase?

后端 未结 4 948
无人及你
无人及你 2020-11-27 23:16

I have a node in Firebase getting continually updated with information from a logfile. The node is lines/ and each child of lines/ is from a

4条回答
  •  余生分开走
    2020-11-27 23:27

    You need to include a timestamp property and run a query.

    // Get the current timestamp
    var now = new Date().getTime();
    // Create a query that orders by the timestamp
    var query = ref.orderByChild('timestamp').startAt(now);
    // Listen for the new children added from that point in time
    query.on('child_added', function (snap) { 
      console.log(snap.val()
    });
    
    // When you add this new item it will fire off the query above
    ref.push({ 
      title: "hello", 
      timestamp: Firebase.ServerValue.TIMESTAMP 
    });
    

    The Firebase SDK has methods for ordering, orderByChild() and methods for creating a range startAt(). When you combine the two you can limit what comes back from Firebase.

提交回复
热议问题