【发布时间】: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 来保存数据,它会很好地工作。
【问题讨论】:
-
希望我的回答能帮助到stackoverflow.com/a/45266181/2073325
标签: node.js amazon-web-services firebase aws-lambda amazon-cloudwatch