Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs app with new Firebase does not retrieve database items

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.

like image 952
DavidG Avatar asked May 22 '16 21:05

DavidG


1 Answers

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!

like image 120
DavidG Avatar answered Oct 08 '22 00:10

DavidG