Amazon Lambda to Firebase

前端 未结 7 1469
甜味超标
甜味超标 2020-11-30 03:42

I get \'Cannot find module \'firebase\' when I try to run this in Lambda (Node.js 4.3)

var Firebase = require(\'firebase\');

Same thing hap

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 03:56

    For me firebase-admin should do the trick. https://firebase.google.com/docs/admin/setup

    Thanks for Josiah Choi for suggesting context.callbackWaitsForEmptyEventLoop though. So lambda doesn't need to initializeFirebase everytimes. My first run was really slow.

      var firebase = require('firebase-admin');
      module.exports.Test = (event, context, callback) => {
    
      context.callbackWaitsForEmptyEventLoop = false;  //<---Important
    
      if(firebase.apps.length == 0) {   // <---Important!!! In lambda, it will cause double initialization.
        firebase.initializeApp({
          credential: firebase.credential.cert("serviceAccount.json"),
          databaseURL: 
        });
    
      }
    
    
     firebase.database().ref('conversation').once('value').then(function(snapshot) {
        console.log (snapshot.val()) ;
       var bodyReturn = {
         input: snapshot.val()
       } ;
    
       callback(null,bodyReturn);
       context.succeed() ;
    });
    
    };
    

提交回复
热议问题