Process timeout | Amazon Lambda to Firebase

前端 未结 4 703
粉色の甜心
粉色の甜心 2021-01-19 11:46

i\'ve written code in node.js and my data is on Firebase. The problem i\'m facing is that my code never exits. I\'ve done it like this one Link

The problem is that f

4条回答
  •  萌比男神i
    2021-01-19 12:15

    Setting callbackWaitsForEmptyEventLoop to false should only be your last resort if nothing else works for you, as this might introduce worse bugs than the problem you're trying to solve here.

    This is what I do instead to ensure every call has firebase initialized, and deleted before exiting.

    // handlerWithFirebase.js
    
    const admin = require("firebase-admin");
    const config = require("./config.json");
    
    function initialize() {
      return admin.initializeApp({
        credential: admin.credential.cert(config),
        databaseURL: "https://.firebaseio.com",
      });
    }
    
    function handlerWithFirebase(func) {
      return (event, context, callback) => {
        const firebase = initialize();
        let _callback = (error, result) => {
          firebase.delete();
          callback(error, result);
        }
        // passing firebase into your handler here is optional
        func(event, context, _callback, firebase /*optional*/);
      }
    }
    
    module.exports = handlerWithFirebase;
    

    And then in my lambda handler code

    // myHandler.js
    
    const handlerWithFirebase = require("../firebase/handler");
    
    module.exports.handler = handlerWithFirebase(
    (event, context, callback, firebase) => {
        ...
    });
    

提交回复
热议问题