Search code examples
angularjsfirebaseionic-frameworkfirebase-realtime-databaseangularfire2

how to get last X min - Angularjs


I have connected my ionic app with firebase, I need to get list which pushed last 5 min ago. I have pushed timestamp in my records in firebase.

I have no any code yet for this. but for example:

this.transactionsRef = db.list('shops');
// Use snapshotChanges().map() to store the key
this.transactions = this.transactionsRef.snapshotChanges().map(changes => {
  return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
});

with this code I can get list of records from firebase. (angularfire2)


Solution

  • You'll want to use a Firebase Database query for that. If you stored the timestamp in a property called timestamp then it should look something like:

    var timestamp = Date.now() - 5 * 60 * 1000; // 5 minutes ago
    var query = this.transactionsRef.orderByChild("timestamp").startAt(timestamp);
    query.snapshotChanges()...
    

    Also see the Firebase documentation on sorting and filtering data.