1

I'm trying to write to my Firebase database using a Twilio function (almost exactly the same as AWS Lambda function, if you're familiar with that), but I'm unable to do so. Firebase is generating a reference key for me, but nothing appears in my Firebase database when I check it. I don't see any error message from my function. Here's what I'm working with.

var firebase = require('firebase');

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

  var firebaseConfigs = {
    apiKey: "[my_api_key]",
    authDomain: "[my_domain].firebaseapp.com",
    databaseURL: "https://[my_domain].firebaseio.com",
    projectId: "[my_project_id]",
    storageBucket: "[my_domain].appspot.com",
    messagingSenderId: "[my_sender_id]"
  };

  if (!firebase.apps.length) {
    firebase.initializeApp(firebaseConfigs);
    console.log('Initialized Firebase app');    
  }

  console.log('saving to firebase');

  var ref = firebase.database().ref().push({
    emailAddress: event.emailAddress,
    test: 'test'
  });

  console.log('saved to ',ref.key)

  setTimeout(function() {}, 3000);

  callback();
};

In my function logs, I see the following:

  • Execution Started
  • Initialized Firebase app
  • saving to firebase
  • saved to [-LdVpr...]
  • Execution ended in 974.16ms using 97 MB

Clearly Firebase is generating a key for me, but the key [-LdVpr...] is not added to my database. What could be happening, and how can I further troubleshoot this?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Shwheelz
  • 545
  • 3
  • 8
  • 22
  • Can you add a catch after push to see if there is an error in writing? `firebase.database().ref().push({ emailAddress: event.emailAddress, test: 'test' }).catch(function(error) { console.error(error); })` – Frank van Puffelen Apr 27 '19 at 23:43

1 Answers1

2

With Execution ended in 974.16ms in the logs, sounds like

setTimeout(function() {}, 3000);
callback(); 

does not really do what you intended to do (wait for 3 seconds and then return to Twilio?).

I would try this ...

setTimeout(function() {
  callback();
}, 3000);

... if that's what you'd like or need to do.


Update after reading this https://firebase.google.com/docs/reference/js/firebase.database.Reference.html#push

push() takes a second parameter, a "Callback called when write to server is complete.", so, I would also try something like this:

var ref = firebase.database().ref().push({
  emailAddress: event.emailAddress,
  test: 'test'
}, function (error) {
  if (error) {
    console.log(error);
    callback();
  } else {
    console.log('Push successful');
    callback();
  }

});

maybe you don't need that 3 seconds thing at all.

Alex Baban
  • 11,312
  • 4
  • 30
  • 44
  • Thank you, this solved my issue, and I'm now able to post to Firebase! I'm still not totally clear on what the issue was - maybe Firebase wasn't waiting long enough before closing the connection, and specifying the callback function helped keep that connection open before the function terminated. Still was odd that Firebase was generating a key for me though. Would love a better explanation about how that happened if anyone has extensive Firebase knowledge and sees this. – Shwheelz Apr 30 '19 at 01:29
  • Glad it helped. I don't have insights of how Firebase implements push() but from what I read I suppose (if I'm not wrong), it is made of two steps, the first one is the one generating the key, the second one committing the transaction. – Alex Baban Apr 30 '19 at 01:44
  • I think what was happening was that your `setTimeout` wasn't having any effect and the Twilio Function was calling the callback and ending before you had the chance to write to Firebase. Instead, only running the Twilio Function callback once Firebase has completed, by doing so within the Firebase callback, means that the action had completed and you could finish the Twilio Function. I'm not sure on the specifics on the Firebase side itself though. – philnash May 02 '19 at 02:10