添加 firebase 条目时 AWS Lambda 函数超时

AWS Lambda function timing out while adding firebase entry

我无法使用 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
};

以上代码在 firebase 中保存数据但超时。

如果我按照 中的说明使用 context.callbackWaitsForEmptyEventLoop = false,它不会超时,但不会保存数据。

请告诉我如何解决这个问题。 cloudwatch中没有有用的信息。

另外一件事,如果我使用 firebase 的 rest api 来保存数据,它运行良好。

问题是你的回调函数

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>});
})