【问题标题】:AWS Lambda function timing out while adding firebase entryAWS Lambda 函数在添加 firebase 条目时超时
【发布时间】:2018-07-20 20:43:44
【问题描述】:

我在使用 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 中,但会超时。

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

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

还有一件事,如果我使用 rest api for firebase 来保存数据,它会很好地工作。

【问题讨论】:

标签: node.js amazon-web-services firebase aws-lambda amazon-cloudwatch


【解决方案1】:

问题是你的回调函数

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

【讨论】:

  • 这很好用。除此之外,我还必须添加 context.callbackWaitsForEmptyEventLoop = false;作为第一个陈述
猜你喜欢
  • 2017-07-22
  • 1970-01-01
  • 2021-10-17
  • 2019-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多