user3871
user3871

Reputation: 12716

Firebase does not work with NodeJS

Following Firebase's tutorial, I was able to start posting data immediately from the frontend, using the cdn:

<script src='https://cdn.firebase.com/js/client/2.2.1/firebase.js'></script>

and:

      var myDataRef = new firebase('https://intense-torch-1234.firebaseio.com/');

      $('#messageInput').keypress(function (e) {
        if (e.keyCode == 13) {
          var name = $('#nameInput').val();
          var text = $('#messageInput').val();
          myDataRef.push({name: name, text: text});
          $('#messageInput').val('');
        }
      });

      myDataRef.on('child_added', function(snapshot) {
        var message = snapshot.val();
        console.log('TESTING', snapshot);
      });

This shows up at my Firebase app location.

I need to use this for NodeJS, so I installed the NPM Firebase module.

I am doing pretty much the same thing here...

var firebase = require('firebase');

exports.handler = function (event, context, callback) {

    var myDataRef = new firebase('https://intense-torch-1234.firebaseio.com/');

    myDataRef.push({name: 'myname', text: 'sometext'});

    myDataRef.on('child_added', function(snapshot) {
        var message = snapshot.val();
        console.log('TESTING', snapshot.val());
    });

The console log fires after my request comes in (from Slack) and shows that the data is coming in. However, it is not updating in my Firebase database.

Are there other configurations I need to do for Node NPM firebase?

Upvotes: 0

Views: 805

Answers (1)

As for other configurations, you need to import the package by using require as per the instructions.

var Firebase = require('firebase');
var myRootRef = new Firebase('https://myprojectname.firebaseIO.com/');
myRootRef.set("hello world!");

There also should be error messages from node for not doing this.

Upvotes: 2

Related Questions