7

I am new to nodejs, but have already a working js client program, in Firebase Version 3.0.2. It turns out I need a server for doing some simple things not possible in a js client.

When I try this basic thing in nodejs, nothing happens - the data in the database is never retrieved, and the node server program continues to run with no errors. The server program (server.js) is as follows:

var firebase = require("firebase");

var myApp =firebase.initializeApp({
    serviceAccount: "xxx/xxx.json",
    databaseURL:  "https://xxx.firebaseio.com"
});


firebase.database().ref().on('value', function(snapshot) {
    console.log("urls value changed");   // NEVER GETS HERE
    console.log(snapshot.val());
});

The database is loaded with data, and this is trying to look at the root. When I add items to the database via the console, still nothing happens. More specific ref() values has no effect.

I'm using node version v4.4.4. The program is invoked at the command line (on a MBP/OSX) with "node server.js".

I have spent hours trying to figure out what I am doing wrong - probably something stupid! Thanks for help in advance.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
DavidG
  • 1,023
  • 11
  • 14
  • Most likely your program is simply exiting before the (asynchronous) `on()` gets the value from the server. So your callback is never invoked. See for example this answer on how to keep the process alive: http://stackoverflow.com/questions/23622051/how-to-forcibly-keep-a-node-js-process-from-terminating – Frank van Puffelen May 22 '16 at 23:57
  • Im having the same issue, I also tested using their quickstart project https://github.com/firebase/quickstart-nodejs/tree/master/database and still no luck. I think it's a bug on their end – garrettmac May 23 '16 at 09:03
  • @FrankvanPuffelen: the process never exits - it appears to be waiting for the database event, but never gets it. – DavidG May 23 '16 at 14:38
  • Attach an error handler, so that you can see if something is going wrong. `firebase.database().ref().on('value', function(snapshot) { ... }, function(error) { console.error(error); })` – Frank van Puffelen May 23 '16 at 15:23
  • With an error handler, it still just sits and does nothing. – DavidG May 23 '16 at 16:48

1 Answers1

8

Fixed!

  1. Added debugging, which is essential:

    firebase.database.enableLogging(true)

  2. Saw that there was an invalid token, and was continually being disconnected/reconnected to try again. Lines in the log like:

    p:0: from server: {"r":2,"b":{"s":"invalid_token","d":"Access denied."}}

    (NOTE: without the logging, this was totally silent!).

  3. It turns out the service account was not correctly made! You must ok terms of service first! To do this: go to https://console.cloud.google.com/

  4. Recreated a new service account and hooked it up - and finally, it works!

DavidG
  • 1,023
  • 11
  • 14