0

I'm a totally new Firebase user, trying to build a simple application -- and I'm running into an odd issue that I haven't been able to find a solution to.

Here's what I'm doing.

I've got a simple Firebase database of users, that looks something like this:

{
  "users": {
    "randomlyAssignedId": {
      "phoneNumber": "+18882223333"
    }
  }
}

The idea is that I'll have one user object (and each user object only contains a phoneNumber field) for each user using my service.

I've got this stored in Firebase and working already.

Now, here's where things are getting tricky. I want to write a query which returns a user by their phoneNumber. So, after reading the Firebase docs, here's what I came up with:

var Firebase = require('firebase');
var users = new Firebase('https://demo.firebaseio.com/users');

users.orderByChild('phoneNumber').equalTo('+18882223333').limitToFirst(1).once('value', function(snapshot) {
  console.log('API call succeeded:', snapshot.val());
}, function(err) {
  console.log('Firebase returned an error:' err);
});

When I run this code sample, the user object is logged to the console successfully (it found the match, yey!), however -- the callback never finishes.

According to the Firebase docs for .once(): https://www.firebase.com/docs/web/api/query/once.html the success (or error callback) should fire exactly ONE time.

This doesn't appear to be happening :(

Could anyone tell me if this is the desired behavior, or if I'm doing something wrong?

Thanks! <3

rdegges
  • 32,786
  • 20
  • 85
  • 109
  • What do you mean when you say "the callback never finishes"? I'm pretty sure the callback will exit after logging, so there's probably something else that you expect to happen and that doesn't. – Frank van Puffelen Nov 08 '15 at 20:29
  • If you run the example app I have there on the command line (with Node), you'll see that after the `console.log` statements fires, the script doesn't end -- it keeps sitting there listening for events indefinitely. – rdegges Nov 09 '15 at 01:48
  • 2
    Ah OK. That is clearer indeed. This is expected behavior, see http://stackoverflow.com/questions/18046639/node-process-doesnt-exit-after-firebase-once – Frank van Puffelen Nov 09 '15 at 02:45
  • Ah, that answered my question. If you post that as an answer I'll accept it =) – rdegges Nov 09 '15 at 03:07
  • 1
    I marked as a duplicate, since it's... a duplicate. :-) – Frank van Puffelen Nov 09 '15 at 03:15

1 Answers1

1

I think what you're expecting is just a little off.

You have 2 functions in your .once() call :

.once("value",function(snapshot){
    ....
},function(err){
   console.log('Did not find a user in the database:');
   console.log(err);
})

However, the 2nd function where you are looking for a possible err value is not going to be utilized if the data is not found. The 2nd function is there to catch a lack of authorization/permission. I think you are looking for something like this:

.once("value",function(snapshot){
    if(snapshot.val()){
       console.log('Found existing user by phoneNumber in database:');
       console.log(snapshot.val());
    }else{
       console.log('Did not find a user in the database:');
       console.log(err);
    }
},function(err){
   console.log('Not authorized');
   console.log(err)
})

If your data (user's phone number) is not found in the database then the result of the snapshot.val() will be null. The 2nd callback function will only contain an err value if you have auth rules set on your firebase and they are not met when making this request.

You can read more here: https://www.firebase.com/docs/web/api/query/once.html

Kyle
  • 1,463
  • 1
  • 12
  • 18
  • 1
    I updated my question to be a bit more explicit. You are right though -- I wasn't handling the errors properly. However, the core problem still persists. If I run this standalone script on the command line -- after console.log'ing some statements, the script never 'exits'. It just hangs there without the process stopping. What I'm saying is that I should haven't to `process.exit(0)` the script unnecessarily since `.once()` should only fire one time. – rdegges Nov 09 '15 at 01:50