Skip to content

V3: orderByChild + startAt or endAt #174

Description

@florianbepunkt

@Salakar Sorry to bother you again. It seems like orderByChild is not working, at least when used with startAt or endAt. I have a list of message objects that I want to query based on their timestamp.

This code returns zero results

firestack
  .database()
  .ref('messages/-KXlqrDgbJg_-FJCoDfE/')
  .orderByChild('createdAt')
  .startAt(1480609558242)
  .once('value')
  .then((snapshot) => {
    console.log('my snap', snapshot);
  })

while this code returns all results

firestack
  .database()
  .ref('messages/-KXlqrDgbJg_-FJCoDfE/')
  .orderByChild('createdAt')
  .endAt(1480609558242)
  .once('value')
  .then((snapshot) => {
    console.log('my snap', snapshot);
  })

Based on my sample data below…both results are wrong. The first example should return the first last two objects, the second example should return the first two objects.

I tried both the v3 branch of this repo as well as master branch of your fork. Based on the sample data the query above should return the first object. But nothing is returned


{
  "-KXlqrDgbJg_-FJCoDfE" : {
    "-KXv8Uvyix9Ns5FeF1O1" : {
      "_id" : "-KXv8Uvyix9Ns5FeF1O1",
      "createdAt" : 1480608580717,
      "text" : "Test123",
      "user" : {
        "_id" : "If2AZYNhruXa4ELtYoviNe7JS2S2"
      },
      "wasSeen" : false
    },
    "-KXvCCjPLePNohIkjSw3" : {
      "_id" : "-KXvCCjPLePNohIkjSw3",
      "createdAt" : 1480609553540,
      "text" : "Test456",
      "user" : {
        "_id" : "If2AZYNhruXa4ELtYoviNe7JS2S2"
      },
      "wasSeen" : false
    },
    "-KXvCDsZq__24IkB6zGs" : {
      "_id" : "-KXvCDsZq__24IkB6zGs",
      "createdAt" : 1480609558242,
      "text" : "Test789",
      "user" : {
        "_id" : "If2AZYNhruXa4ELtYoviNe7JS2S2"
      },
      "wasSeen" : false
    },
    "-KXvQisiS1bsm3oYCdll" : {
      "_id" : "-KXvQisiS1bsm3oYCdll",
      "createdAt" : 1480613359327,
      "text" : "Test101112",
      "user" : {
        "_id" : "If2AZYNhruXa4ELtYoviNe7JS2S2"
      },
      "wasSeen" : false
    }
  }
}

Activity

  1. changed the title [-]V3: orderByChild[/-] [+]V3: orderByChild + startAt or endAt[/+] on Dec 1, 2016
  2. Salakar commented on Dec 2, 2016

    @Salakar
    Collaborator

    Hmm weird one @florianbepunkt - database in the current pushed up state of my branch has not been touched bar a PR from @BlooJeans which was on master that I manually pulled in.

    Will look at this today and get back to you, the example helps, cheers!

  3. florianbepunkt commented on Dec 2, 2016

    @florianbepunkt
    Author

    @Salakar Maybe this is of interest #82 – it was not working properly on master until recently

  4. Salakar commented on Dec 2, 2016

    @Salakar
    Collaborator

    @florianbepunkt looks like @chrisbianca fixed this issue 3 days ago on my branch - have you tried pulling the latest?

    This commit:
    Salakar@ec9f488

  5. florianbepunkt commented on Dec 2, 2016

    @florianbepunkt
    Author

    @Salakar I did. To make sure I just installed it again via npm install --save https://github.com/Salakar/react-native-firestack.git

    The commit only changes things on android side… I see this problem with iOS.

  6. chrisbianca commented on Dec 2, 2016

    @chrisbianca
    Contributor

    I only updated the Android side as that's where I'd noticed the issue.
    Just taken a look and a similar sort of change does need to happen on the iOS side - I'll see if I can take a look in a little while...

  7. Salakar commented on Dec 2, 2016

    @Salakar
    Collaborator

    @florianbepunkt oh ok my bad, thought you were having an android issue sorry.

    @chrisbianca that would be awesome thanks!

  8. florianbepunkt commented on Dec 2, 2016

    @florianbepunkt
    Author

    I'm not sure if this is related… using a fresh install from Salakar's fork I see the same issue on android.

    Using my sample data

    
    firestack
      .database()
      .ref('messages/-KXlqrDgbJg_-FJCoDfE/')
      .orderByChild('createdAt')
      .startAt(1480609558242)
      .once('value')
      .then((snapshot) => {
        console.log('my snap', snapshot);
      })
    
    

    returns zero values…

  9. chrisbianca commented on Dec 2, 2016

    @chrisbianca
    Contributor

    Ok, I don't have any data setup to test this with at the moment.
    When I get some time later on, I'll take a look and see if I can figure out what's going on.

  10. florianbepunkt commented on Dec 2, 2016

    @florianbepunkt
    Author

    @chrisbianca okay. I throw together a simple test project and upload it if that helps

  11. chrisbianca commented on Dec 2, 2016

    @chrisbianca
    Contributor

    @florianbepunkt that would be great!

  12. florianbepunkt commented on Dec 2, 2016

    @florianbepunkt
    Author

    @chrisbianca @Salakar Okay, here we go… https://github.com/florianbepunkt/fireStackQuery

    you can clone the repo, run npm install and run it. I've tested it on ios and it reproduces the issue.

  13. chrisbianca commented on Dec 2, 2016

    @chrisbianca
    Contributor

    @florianbepunkt @Salakar

    So I've figured out why it's broken in Java... It's passing the startAt and endAt as a String rather than a number. The fix is an awful lot of repetitive code but I can't think of a better way to work around it! Just pushed the fix to the v3 branch and sorted for boolean types too.

    Will try and look at iOS now...

  14. florianbepunkt commented on Dec 2, 2016

    @florianbepunkt
    Author

    @chrisbianca great! just checked android version with your commit

  15. chrisbianca commented on Dec 3, 2016

    @chrisbianca
    Contributor

    I've just committed the equivalent fix for iOS. I rewrote a large part of the iOS database module to bring it more in line with Android and stop regenerating firebase references unnecessarily. Let me know if you have any other issues...

  16. 10 remaining items

  17. florianbepunkt commented on Dec 6, 2016

    @florianbepunkt
    Author

    @BlooJeans Thanks for the clarification. Makes sense.

  18. Salakar commented on Dec 6, 2016

    @Salakar
    Collaborator

    @florianbepunkt @chrisbianca - @Ehesp and I are having the same issue at the moment:

    firestack
        .database()
        .ref('matches')
        .orderByChild('startsAt')
        .startAt(1480982400)
        .endAt(1481068799)
        .on('value', snap => console.log(snap.val()));

    Returns no results on firestack. The exact same query however on the web sdk returns correctly and gives matches all for that time range.

    We're using the head of my fork which includes your changes @chrisbianca - am i missing something?

    A possibly better way to handle the value types would be get the types on the JS side i.e. it gets sent over as startsAt:number:1234567890 then we don't need to do any guess work on either ios / android - thoughts?

    Sorry for the slow responses also, have been ill, urgh.

  19. reopened this on Dec 6, 2016
  20. florianbepunkt commented on Dec 6, 2016

    @florianbepunkt
    Author

    @Salakar I can confirm (sorry @chrisbianca didn't notice since I had no need to combine startAt and endAt yet)

    although I get results, when I combine startAt and endAt

    both queries below return the same results. updated my test project https://github.com/florianbepunkt/fireStackQuery

    firestack
          .database()
          .ref('messages/-KXlqrDgbJg_-FJCoDfE/')
          .orderByChild('createdAt')
          .endAt(1480609558242)
          .once('value')
          .then((snapshot) => {
            console.log('queryTest orderByChild endAt test > snapshot', snapshot);
          })
    
        firestack
          .database()
          .ref('messages/-KXlqrDgbJg_-FJCoDfE/')
          .orderByChild('createdAt')
          .startAt(1480609558242)
          .endAt(1480617793311)
          .once('value')
          .then((snapshot) => {
            console.log('queryTest orderByChild startAt and endAt test > snapshot', snapshot);
          })
    
  21. chrisbianca commented on Dec 6, 2016

    @chrisbianca
    Contributor

    @florianbepunkt thanks, we've been discussing off this thread and I'm just working on a fix

  22. florianbepunkt commented on Dec 6, 2016

    @florianbepunkt
    Author

    @chrisbianca alright. just fyi: I tested above only on ios. if I can help in any way let me know.

  23. chrisbianca commented on Dec 6, 2016

    @chrisbianca
    Contributor

    @florianbepunkt I've just pushed a fix which I've tested on iOS. Would be great if you're able to confirm that it's working for you now too

  24. florianbepunkt commented on Dec 6, 2016

    @florianbepunkt
    Author

    @chrisbianca works on ios. tested orderByChild with startAt, endAt and both combined.

  25. chrisbianca commented on Dec 6, 2016

    @chrisbianca
    Contributor

    Excellent! @Salakar when you're happy, I think this can be closed again

  26. Ehesp commented on Dec 6, 2016

    @Ehesp
    Contributor

    Also works on my end. Nice one 👍 (Android)

  27. Salakar commented on Dec 7, 2016

    @Salakar
    Collaborator

    Closing issue as it's now resolved completely 👍

  28. esdrasportillo commented on Feb 21, 2017

    @esdrasportillo

    There is something bizarre happening here, this same error is happening only in the iPhone 5, both in the simulator and on a real device as well, I am on Salakar's branch, has anyone ever tested on an iPhone 5 before and got this error?

  29. AndrewHenderson commented on Mar 25, 2017

    @AndrewHenderson

    I am still not getting the expected results.

    firestack.database
          .ref('events')
          .orderByChild('timestamp')
          .endAt(1490331248253)
          .once('value')
          .then((snapshot) => {
            console.log('queryTest orderByChild endAt test > snapshot', snapshot);
          })

    This returns all results rather than stopping at the timestamp provided.

    The following provides no results at all despite having records in the database that have those exact timestamps:

    firestack
          .database
          .ref('events')
          .orderByChild('timestamp')
          .startAt(1490331248253 )
          .endAt(1490331422094)
          .once('value')
          .then((snapshot) => {
            console.log('queryTest orderByChild startAt and endAt test > snapshot', snapshot);
          })

    @chrisbianca What push are you referring to? Can you link to the commit?

    ping: @Salakar

  30. Salakar commented on Mar 27, 2017

    @Salakar
    Collaborator

    @AndrewHenderson the above mentioned fixes happened on the v3 version of firestack, your code above looks like you're currently using the old firestack version.

    e.g. database became database() on the new version to match the web sdk.

    v3 was my fork: https://github.com/Salakar/react-native-firestack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions