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.
Fixed!
Added debugging, which is essential:
firebase.database.enableLogging(true)
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!).
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/
Recreated a new service account and hooked it up - and finally, it works!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With