2017-09-16 38 views
5

Google Firebase'den veri listelerken veya veri alırken, toplanan verileri nasıl arayabiliriz? Örnek olarak ,Firebase'de nasıl sayfalayabiliriz?

countries = [ 
    {name: 'Afghanistan', code: 'AF'}, 
    {name: 'Åland Islands', code: 'AX'}, 
    {name: 'Albania', code: 'AL'}, 
... 
] 

Sayfa başına 10 olarak listelemek istediğiniz ve ben Örnek olarak page =0 with size 10 veya page=5 with size=5

+0

Bu soru çoktan ele alınmıştı. [Diğer soruları sordu] (https://www.google.com/search?q=site:stackoverflow.com+firebase+pagination) okudunuz mu? Eğer değilse, ilk önce bunu yapmanızı ve eğer takılırsanız daha fazla ayrıntıyla birlikte rapor etmenizi öneririm. –

+2

Son yanıt: https://stackoverflow.com/questions/45914345/how-to-read-firebase-database/45920801#45920801 –

+0

Sorunumu kapsamıyor ... Herhangi bir çözüm var mı? –

cevap

2

almak istiyorsanız

{ 
    -KBZIPRqYmrRgNZ3GJt6: { asc: 1, desc: 9, name: "Rusty Kovacek"}, 
    -KBZIPRvieZbW-k9R9ra: { asc: 2, desc: 8, name: "Lloyd Feil" }, 
    -KBZIPRvieZbW-k9R9rc: { asc: 3, desc: 7, name: "Jasmin Hilll" }, 
    -KBZIPRwiXUgOtv3fCAL: { asc: 4, desc: 6, name: "Ms. Ibrahim Schinner" }, 
    -KBZIPRwiXUgOtv3fCAN: { asc: 5, desc: 5, name: "Dorothea Koepp" }, 
    -KBZIPRxpCAUyo5TJmY3: { asc: 6, desc: 4, name: "Melvin Marquardt" }, 
    -KBZIPRxpCAUyo5TJmY5: { asc: 7, desc: 3, name: "Celestine Bode" }, 
    -KBZIPRy5Uvz9wUOa6Jx: { asc: 8, desc: 2, name: "Emerald Olson" }, 
    -KBZIPRy5Uvz9wUOa6Jz: { asc: 9, desc: 1, name: "Miss Joey Jacobi" }, 
    -KBZIPRzRhuguDLLftQR: { asc: 10, desc: 0, name: "Ms. Denis Rutherford" } 
} 

ve

var axios = require('axios'); 
var Firebase = require('firebase'); 
var namesRef = new Firebase('https://demos-firebase.firebaseio.com/dataDemo/names'); 


axios.get(namesRef.toString() + '.json?shallow=true') 
    .then(function (res) { 
    // This list is not sorted!!! 
    // res.data = { 
    // '-KBZIPRqYmrRgNZ3GJt6': true, 
    // '-KBZIPRwiXUgOtv3fCAN': true, 
    // '-KBZIPRy5Uvz9wUOa6Jx': true, 
    // '-KBZIPRzRhuguDLLftQR': true, 
    // '-KBZIPRxpCAUyo5TJmY5': true, 
    // '-KBZIPRxpCAUyo5TJmY3': true, 
    // '-KBZIPRwiXUgOtv3fCAL': true, 
    // '-KBZIPRvieZbW-k9R9ra': true, 
    // '-KBZIPRvieZbW-k9R9rc': true, 
    // '-KBZIPRy5Uvz9wUOa6Jz': true 
    // } 
    var keys = Object.keys(res.data).sort(); // Notice the .sort()! 
    var pageLength = 2; 
    var pageCount = keys.length/pageLength; 
    var currentPage = 1; 
    var promises = []; 
    var nextKey; 
    var query; 

    for (var i = 0; i < pageCount; i++) { 
     key = keys[i * pageLength]; 
     console.log('key', key); 
     query = namesRef.orderByKey().limitToFirst(pageLength).startAt(key); 
     promises.push(query.once('value')); 
    } 

    Promise.all(promises) 
     .then(function (snaps) { 
     var pages = []; 
     snaps.forEach(function (snap) { 
      pages.push(snap.val()); 
     }); 
     console.log('pages', pages); 
     process.exit(); 
     // pages = [{ 
     // '-KBZIPRqYmrRgNZ3GJt6': { 
     //  asc: 1, 
     //  desc: 9, 
     //  name: 'Rusty Kovacek' 
     // }, 
     // '-KBZIPRvieZbW-k9R9ra': { 
     //  asc: 2, 
     //  desc: 8, 
     //  name: 'Lloyd Feil' 
     // } 
     // }, { 
     // '-KBZIPRvieZbW-k9R9rc': { 
     //  asc: 3, 
     //  desc: 7, 
     //  name: 'Jasmin Hilll' 
     // }, 
     // '-KBZIPRwiXUgOtv3fCAL': { 
     //  asc: 4, 
     //  desc: 6, 
     //  name: 'Ms. Ibrahim Schinner' 
     // } 
     // }, { 
     // '-KBZIPRwiXUgOtv3fCAN': { 
     //  asc: 5, 
     //  desc: 5, 
     //  name: 'Dorothea Koepp' 
     // }, 
     // '-KBZIPRxpCAUyo5TJmY3': { 
     //  asc: 6, 
     //  desc: 4, 
     //  name: 'Melvin Marquardt' 
     // } 
     // }, { 
     // '-KBZIPRxpCAUyo5TJmY5': { 
     //  asc: 7, 
     //  desc: 3, 
     //  name: 'Celestine Bode' 
     // }, 
     // '-KBZIPRy5Uvz9wUOa6Jx': { 
     //  asc: 8, 
     //  desc: 2, 
     //  name: 'Emerald Olson' 
     // } 
     // }, { 
     // '-KBZIPRy5Uvz9wUOa6Jz': { 
     //  asc: 9, 
     //  desc: 1, 
     //  name: 'Miss Joey Jacobi' 
     // }, 
     // '-KBZIPRzRhuguDLLftQR': { 
     //  asc: 10, 
     //  desc: 0, 
     //  name: 'Ms. Denis Rutherford' 
     // } 
     // }] 

     }); 
    });