Can't get Firebase in Node JS app to run

I am trying to set up a firebase JS client using NodeJS. So far my code is

var firebase = require('firebase/app');

require('firebase/database');

var config = {
  apiKey: "MY_SECRET_KEY_fhcWICPI",
  authDomain: "my_fir_app.firebaseapp.com",
  databaseURL: "https://my_fir_app.firebaseio.com",
};
var firApp = firebase.initializeApp(config);

firebase.database.enableLogging(true)

// Get a reference to the database service
var database = firebase.database();

      

Then, here is one of my Firebase functions to persist data to a realtime database.

/**
 * This will save the authors of stories in Firebase
 * @param  {String} id              The ID of the author
 * @param  {String} firstName       The authors first name
 * @param  {String} lastName        The authors last name
 * @param  {Timestamp} dateCreated  The unix time stamp when the author    was created
 */
 function saveStoryAuthor(id, firstName, lastName, dateCreated) {
    database.ref('mystoriesdb/authors/' + id).set({
      first_name: firstName,
      last_name: lastName,
      date_created : dateCreated
    });
 }

      

Finally, somewhere in the middle of my code, I call this function as

...

saveStoryAuthor('MZ8XWXNrkG', 'Dennis', 'Richie')

...

      

However, this is what I am getting in the logs (since I enabled logging)

$ node index.js
p:0: Browser went online.
p:0: Making a connection attempt
getToken() completed. Creating connection.
c:0:0: Connection created
p:0: Failed to get token: Error: No transports available
p:0: data client disconnected
p:0: Trying to reconnect in 326.9669258513522ms
0: onDisconnectEvents
p:0: Making a connection attempt
getToken() completed. Creating connection.
c:0:1: Connection created
p:0: Failed to get token: Error: No transports available

      

I am probably doing something wrong. Can someone help.

+3


source to share


3 answers


You don't seem to have created a service account to add firebase to your project using node js

Check out the documentation here .



var admin = require("firebase-admin");

var serviceAccount = require("path/to/serviceAccountKey.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
});

      

+1


source


You need to change

var firebase = require('firebase/app');

      



to

var firebase = require('firebase');

      

+1


source


Solution: Add the following to your webpack root config:

resolve: {
    mainFields: ['main']
}

      


Explanation:

The package firebase/database

defines various entry points : main

, browser

and module

. NodeJS uses main

. Webpack is a hipster and uses module

.

The guys at Firebase did some specific NodeJS configuration (such as setting up WebSocket as transport layer) at the entry point defined main

. So after linking, this NodeJS custom code is not configured, so it doesn't work. Saying Webpack just uses it main

like Node solves this problem.

0


source







All Articles