Home > OS >  Firebase Database Rest Api: EndAt() Returns Duplicate Result When multiple nodes with same value
Firebase Database Rest Api: EndAt() Returns Duplicate Result When multiple nodes with same value

Time:11-30

I hope you have a good day

I'm using the Firebase Database Rest API to fetch the first top 10 posts ordered by (Views) and When I fetch the first 10 posts, there is no problem Like This:

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

{
  "-Mn7-ZxkUPO01ifh2tpEn" : {
    "Text" : "Im 11",
    "Title" : "some title",
    "Views" : 11
  },
  "-Mn7-ZxkUPO01ifh2txpEn" : {
    "Text" : "Im 11 Two",
    "Title" : "some title",
    "Views" : 11
  },
  "-Mn7-ZxkUPO01ifhds2tpEn" : {
    "Text" : "some text",
    "Title" : "some title",
    "Views" : 13
  },
  "-Mn7-ZxkUPO01ifhewtpEn" : {
    "Text" : "some text",
    "Title" : "some title",
    "Views" : 14
  },
  "-Mn7-ZxkUPO01ifhtdxpEn" : {
    "Text" : "some text",
    "Title" : "some title",
    "Views" : 15
  },
  "-Mn7-ZxkUPO01ifhtp11En" : {
    "Text" : "some text",
    "Title" : "some title",
    "Views" : 16
  },
  "-Mn7-ZxkUPO01sdifhtpEn" : {
    "Text" : "some text",
    "Title" : "some title",
    "Views" : 17
  },
  "-Mn7-ZxkUPOd01ifh2tpEn" : {
    "Text" : "some text",
    "Title" : "some title",
    "Views" : 18
  },
  "3342323231" : {
    "Text" : "d",
    "Title" : 24,
    "Views" : 19
  },
  "444444444444" : {
    "Text" : "dd",
    "Title" : "xzx",
    "Views" : 20
  }
}

And when I sort first 10 elements in order of highest Views The last id will be -Mn7-ZxkUPO01ifh2txpEn And when I fetch another 11 elements starting from the LastId and LastValue it will return the result of a duplicate data because there are two elements with the same value of (Views) but it is supposed to not do that because I have passed the LastValue as well as the LastId Like this:

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

I expected it would return a duplicate result which is the last element only -Mn7-ZxkUPO01ifh2txpEn I can remove it easily but why does it return other elements that were shown in the first 10 ?

I tried not to make any item contain a duplicate value and the results are correct and there are no duplicate posts So what is the solution to this problem?

Sorry for the long post, and thank you :)

CodePudding user response:

When you get the final page of results with https://ameen-66522.firebaseio.com/Main/News/Categories/Education/.json?print=pretty&orderBy="Views"&limitToLast=10, the first two nodes in the results are:

"-Mn7-ZxkUPO01ifh2tpEn" : {
  "Text" : "Im 11",
  "Views" : 11
},
"-Mn7-ZxkUPO01ifh2txpEn" : {
  "Text" : "Im 11 Two",
  "Views" : 11
},

In the call in your question you're passing in the -Mn7-ZxkUPO01ifh2txpEn key, which is the second key from the results you already had. So that's then also where Firebase starts/ends the slice of data it returns.

If you want only one overlapping item, use key -Mn7-ZxkUPO01ifh2tpEn: https://ameen-66522.firebaseio.com/Main/News/Categories/Education/.json?print=pretty&orderBy="Views"&limitToLast=11&endAt=11,"-Mn7-ZxkUPO01ifh2tpEn"

This will then give you these results:

{
  "-Mn7-ZxkUPO01dxifhtpEn" : {
    "Text" : "some text",
    "Views" : 1
  },
  "-Mn7-ZxkUPO01iddfh2tpEn" : {
    "Text" : "some text",
    "Views" : 2
  },
  "-Mn7-ZxkUPO01idsdfh2tpEn" : {
    "Text" : "some text",
    "Views" : 3
  },
  "-Mn7-ZxkUPO01idsdfhtpEn" : {
    "Text" : "some text",
    "Views" : 4
  },
  "-Mn7-ZxkUPO01idxfh2tpEn" : {
    "Text" : "must 5",
    "Views" : 6
  },
  "-Mn7-ZxkUPO01ifddhtpEn" : {
    "Text" : "some text",
    "Views" : 5
  },
  "-Mn7-ZxkUPO01ifdshtpEn" : {
    "Text" : "must 7",
    "Views" : 7
  },
  "-Mn7-ZxkUPO01ifh21tpEn" : {
    "Text" : "some text",
    "Views" : 8
  },
  "-Mn7-ZxkUPO01ifh2dstpEn" : {
    "Text" : "some text",
    "Views" : 9
  },
  "-Mn7-ZxkUPO01ifh2tpEdsn" : {
    "Text" : "some text",
    "Views" : 10
  },
  "-Mn7-ZxkUPO01ifh2tpEn" : {
    "Text" : "Im 11",
    "Views" : 11
  }
}

I recommend using more readable and recognizable keys in a repro such as this, as it's quite easy to overlook the difference in those two keys in the first snippet.

  • Related