Home > OS >  Firebase Database Rest API: Returns incorrect results when all nodes contain the same value except o
Firebase Database Rest API: Returns incorrect results when all nodes contain the same value except o

Time:12-01

I have a Json data that is sorted by highest viewed by using

OrderBy("Views").LimitToLast(10)

All elements have the same (Views) value except one.

This is the Full Json data Without adding a specific limit:

https://ameen-66522.firebaseio.com/Main/News/Categories/Education/.json?print=pretty&orderBy="Views"

{
  "-Mn7-ZxkUPO01dxifhtpEn" : {
    "Text" : 1,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01iddfh2tpEn" : {
    "Text" : 2,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01idsdfh2tpEn" : {
    "Text" : 3,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01idsdfhtpEn" : {
    "Text" : 4,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01idxfh2tpEn" : {
    "Text" : 5,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01ifddhtpEn" : {
    "Text" : 6,
    "Title" : "im different value",
    "Views" : 50
  },
  "-Mn7-ZxkUPO01ifdshtpEn" : {
    "Text" : 7,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01ifh21tpEn" : {
    "Text" : 8,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01ifh2dstpEn" : {
    "Text" : 9,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01ifh2tpEdsn" : {
    "Text" : 10,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01ifh2tpEn" : {
    "Text" : 11,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01ifh2txpEn" : {
    "Text" : 12,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01ifhds2tpEn" : {
    "Text" : 13,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01ifhewtpEn" : {
    "Text" : 14,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01ifhtdxpEn" : {
    "Text" : 15,
    "Title" : "some title",
    "Views" : 20
  }
}

When I use

OrderBy("Views").LimitToLast(10)

It will fetch the top 10 elements correctly and the Last Id will be

-Mn7-ZxkUPO01ifddhtpEn

It is the one that has a different value from the rest of the data

Here the Response result :

https://ameen-66522.firebaseio.com/Main/News/Categories/Education/.json?print=pretty&orderBy="Views"&limitToLast=10

Now it is supposed that when I fetch the remaining data starting from the lastId

-Mn7-ZxkUPO01ifddhtpEn

and

LastValue

50

It will return the remaining data correctly even in fact it will return data that was fetched in the first 10 and this problem occurs when all the data carry the same value of views except for only one different.

https://ameen-66522.firebaseio.com/Main/News/Categories/Education/.json?print=pretty&orderBy="Views"&limitToLast=11&endAt=50,"-Mn7-ZxkUPO01ifddhtpEn"

{
  "-Mn7-ZxkUPO01idxfh2tpEn" : {
    "Text" : 5,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01ifddhtpEn" : {
    "Text" : 6,
    "Title" : "some title",
    "Views" : 50
  },
  "-Mn7-ZxkUPO01ifdshtpEn" : {
    "Text" : 7,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01ifh21tpEn" : {
    "Text" : 8,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01ifh2dstpEn" : {
    "Text" : 9,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01ifh2tpEdsn" : {
    "Text" : 10,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01ifh2tpEn" : {
    "Text" : 11,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01ifh2txpEn" : {
    "Text" : 12,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01ifhds2tpEn" : {
    "Text" : 13,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01ifhewtpEn" : {
    "Text" : 14,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01ifhtdxpEn" : {
    "Text" : 15,
    "Title" : "some title",
    "Views" : 20
  }
}

it is supposed to return :

{
  "-Mn7-ZxkUPO01dxifhtpEn" : {
    "Text" : 1,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01iddfh2tpEn" : {
    "Text" : 2,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01idsdfh2tpEn" : {
    "Text" : 3,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01idsdfhtpEn" : {
    "Text" : 4,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01idxfh2tpEn" : {
    "Text" : 5,
    "Title" : "some title",
    "Views" : 20
  },
  "-Mn7-ZxkUPO01ifddhtpEn" : {
    "Text" : 6,
    "Title" : "some title",
    "Views" : 50
  }

Thanks for the help :) I appreciate your work

CodePudding user response:

As far as I can tell, this node is the last one in your JSON when you order by view count:

"-Mn7-ZxkUPO01ifddhtpEn" : {
  "Text" : 6,
  "Title" : "some title",
  "Views" : 50
},

So when you do this query: orderBy="Views"&limitToLast=10, you're asking for the last 10 nodes when ordered by Views count.

You then use the key of the last node in your next query: orderBy="Views"&limitToLast=11&endAt=50,"-Mn7-ZxkUPO01ifddhtpEn", so now you're asking for the last 11 nodes ending at the last node already. That endAt clause is superfluous as far as I can see, to this is the same as orderBy="Views"&limitToLast=11.

The thing to keep in mind here is that you're paginating backwards from the end of your data set. So to get the second page from the bottom, you need to know the Views key of the first item on the last page.

  • Related