1

I would like to get Most Popular posts with highest number of UpVotes one after another.

this.nextPostObservableList=  this.af.database.list('/listOfPost',{
  query :{
    orderByChild : 'postUpvote',
    limitToLast : 2,
    endAt : this.currentPost.postUpvote
  }
});

In this case if I orderByChild with postUpvote, I am able to navigate one after another. But if Multiple Posts have same upVote count then I am not able to navigate (Greater than 2 as per above code snippet). Beyond post2 in the below scenario. Please suggest a solution.

{
  "post1" : {
    "postUpvote" : 10,
    "postname" : "sample 1"
  },
  "post2" : {
    "postUpvote" : 10,
    "postname" : "Sample 2"
  },
  "post3" : {
    "postUpvote" : 20,
    "postname" : "sample3"
  },
  "post4" : {
    "postUpvote" : 10,
    "postname" : "sample4"
  }
}
cartant
  • 57,105
  • 17
  • 163
  • 197
suvankar bose
  • 291
  • 2
  • 12
  • Hmm... the Firebase SDKs have a `endAt` that takes an extra parameter (the key of the item to start at) for this purpose. I'm not sure if that's wrapped in AngularFire2 though. – Frank van Puffelen Jan 22 '17 at 14:31
  • Hi Frank as per documentation key param in endAt is only Valid if is OrderBy is done using priority . :( . [link](https://firebase.google.com/docs/reference/js/firebase.database.Query) – suvankar bose Jan 22 '17 at 15:17
  • I'm pretty sure that I've tested the key param with orderByChild(). Can you give it a try? – Frank van Puffelen Jan 22 '17 at 18:32
  • @FrankvanPuffelen - Yep, the optional parameters for `startAt` and `endAt` are not supported in AngularFire2: https://github.com/angular/angularfire2/issues/362 (and the relevant source is [here](https://github.com/angular/angularfire2/blob/2.0.0-beta.7/src/database/firebase_list_factory.ts#L72-L78)) – cartant Jan 23 '17 at 00:38
  • Yes its seems it is not supported yet . Let me try using firebase javascript sdk directly . – suvankar bose Jan 23 '17 at 15:01

1 Answers1

3

Support for the optional key parameter that can be passed to startAt was added in AngularFire2 2.0.0-beta.7. At that time, the Firebase SDK documentation for endAt and equalTo stated that the optional key parameter for those methods could be used only when ordering by priority.

The documentation has since been updated and states that it is supported when ordering child, value or priority.

A PR adding support for the key parameter for AngularFire2 when using endAt or equalTo has been merged, so the next release will support the specifying a key when ordering by child and using endAt.

To specify the optional key, you would use an object literal containing the value and the key, so your query to retreive the current post and its preceding post would be something like this:

this.nextPostObservableList = this.af.database.list('/listOfPost', {
  query :{
    orderByChild : 'postUpvote',
    limitToLast : 2,
    endAt : {
      value : this.currentPost.postUpvote,
      key : this.currentPost.postUpvote.$key
    }
  }
});
cartant
  • 57,105
  • 17
  • 163
  • 197
  • Is this case supported for empty string value? Meaning elements that have value regardless value itself, and starting/ending by given key parameter. (one test I did was unsuccessful, data returned contained elements from the start of the list, not from given key) – Bogac Mar 13 '17 at 07:11
  • I'm not sure I understand what you are saying re: empty strings, as an empty string is a valid value. If it's not working for you, you could ask a question on SO or raise an issue on GitHub. Either way, I'll likely see it. There's unlikely to be enough space in these comments to discuss it. – cartant Mar 13 '17 at 07:26
  • Getting exception that endAt and startAt cannot take object as argument . – suvankar bose Apr 08 '17 at 08:41
  • The optional key was added for `startAt` in [2.0.0-beta-7](https://github.com/angular/angularfire2/pull/821) and for `endAt` in [2.0.0-beta-8](https://github.com/angular/angularfire2/pull/838). Please make sure you have the most recent version (2.0.0-beta-8) installed. – cartant Apr 08 '17 at 09:53