Node.js 添加firebase条目时AWS Lambda函数超时

Node.js 添加firebase条目时AWS Lambda函数超时,node.js,amazon-web-services,firebase,aws-lambda,amazon-cloudwatch,Node.js,Amazon Web Services,Firebase,Aws Lambda,Amazon Cloudwatch,我在使用lambda函数保存firebase数据库中的数据时遇到问题。它超时了。我尝试将超时设置为5分钟,理想情况下,它不应该执行,但仍然超时 'use strict'; var firebase = require('firebase'); exports.handler = (event, context, callback) => { console.log(context); var params = JSON.stringify(event);

我在使用lambda函数保存firebase数据库中的数据时遇到问题。它超时了。我尝试将超时设置为5分钟,理想情况下,它不应该执行,但仍然超时

'use strict';

var firebase = require('firebase');

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

    console.log(context);

    var params = JSON.stringify(event);

    var config = {
    apiKey: "SECRETAPIKEY",
    authDomain: "myapplication.firebaseapp.com",
    databaseURL: "https://myapplication.firebaseio.com",
    storageBucket: "myapplication.appspot.com",
    messagingSenderId: "102938102938123"
    };

    if(firebase.apps.length === 0) {   // <---Important!!! In lambda, it will cause double initialization.
        firebase.initializeApp(config);
    }

    var db = firebase.database();

    var postData = {
    username: "test",
    email: "test@mail.com"
    };

    // Get a key for a new Post.
    var newPostKey = firebase.database().ref().child('posts').push().key;

    // Write the new post's data simultaneously in the posts list and the user's post list.
    var updates = {};
    updates['/posts/' + newPostKey] = postData;

    callback(null, {"Hello": firebase.database().ref().update(updates)}); // SUCCESS with message
};
“严格使用”;
var firebase=require('firebase');
exports.handler=(事件、上下文、回调)=>{
console.log(上下文);
var params=JSON.stringify(事件);
变量配置={
apiKey:“SECRETAPIKEY”,
authDomain:“myapplication.firebaseapp.com”,
数据库URL:“https://myapplication.firebaseio.com",
storageBucket:“myapplication.appspot.com”,
messagingSenderId:“102938102938123”
};

如果(firebase.apps.length==0){/则问题在于您的回调函数

callback(null, {"Hello": firebase.database().ref().update(updates)}); // SUCCESS with message
在Firebase进行更新之前调用

您应该将回调函数放在Firebase更新回调中,而不是放在当前回调中:

firebase.database().ref().update(updates, function (err) {

    // your processing code here

    callback(null, {<data to send back>});
})
firebase.database().ref().update(更新,函数(err){
//您的处理代码在这里
回调(null,{});
})

这很好。除此之外,我还必须添加context.callbackhaitsforemptyeventloop=false;作为第一句话,希望我在这里的回答能有所帮助