Reputation: 718
See below code. I'm working on an AWS lambda function that will push data to a firebase database. My problem is that the only way I've been able to get it to work is to put a setTimeout on the callback function inside of my handler. I don't understand why this is required. Doesn't the request get sent off to firebase before the callback starts.
This doesn't work.
var admin = require("firebase-admin");
admin.initializeApp({
credential: admin.credential.cert("./info.json"),
databaseURL: "https://app.firebaseio.com"
});
exports.handler = (event, context, callback) => {
let x = admin.database().ref(`/12851/Winners`);
x.push(525555);
callback(null, {Winner: true});
};
This does work though.
var admin = require("firebase-admin");
admin.initializeApp({
credential: admin.credential.cert("./info.json"),
databaseURL: "https://app.firebaseio.com"
});
exports.handler = (event, context, callback) => {
let x = admin.database().ref(`/12851/Winners`);
x.push(525555);
setTimeout(()=>{
callback(null, {Winner: true});
},1500)
};
Upvotes: 0
Views: 677
Reputation: 28750
Wire into a .then
instead since .push
returns a promise:
var admin = require("firebase-admin");
admin.initializeApp({
credential: admin.credential.cert("./info.json"),
databaseURL: "https://app.firebaseio.com"
});
exports.handler = (event, context, callback) => {
let x = admin.database().ref(`/12851/Winners`);
x.push(525555).then(function(){
callback(null, {Winner: true});
});
};
Upvotes: 4