Skip to content

Instantly share code, notes, and snippets.

@yimengtianya
Last active February 25, 2018 05:33
Show Gist options
  • Save yimengtianya/c933134c17567779338a to your computer and use it in GitHub Desktop.
Save yimengtianya/c933134c17567779338a to your computer and use it in GitHub Desktop.
Wilddog:只获取新增的数据
// when the records is empty ,it not work
var ref = new Wilddog(...);
var first = true;
ref.limitToLast(1).on("child_added", function(snap) {
if( first ) {
first = false;
}
else {
console.log('new record', snap.key());
}
});
var ref = new Wilddog(...);
ref.once("value", function(snap) {
var keys = Object.keys(snap.val()||{});
var lastIdInSnapshot = keys[keys.length-1]
ref.orderByKey().startAt(lastIdInSnapshot).on("child_added", function(newMessSnapshot) {
if( snap.key() === lastIdInSnapshot ) { return; }
console.log('new record', newMessSnapshot.key());
})
})
// assumes you add a timestamp field to each record (see Wilddog.ServerValue.TIMESTAMP)
// pros: fast and done server-side (less bandwidth, faster response), simple
// cons: a few bytes on each record for the timestamp
var ref = new Wilddog(...);
ref.orderByChild('timestamp').startAt(Date.now()).on('child_added', function(snapshot) {
console.log('new record', snapshot.key());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment