Hey,
I am using a netlify function to write an entry to Firebase Firestore whenever someone sent his/ her email in the contact form. In my tests the lambda-function is reached and while using netlify dev on my local machine firebase firestore is also reached perfectly and the data added to the database.
Using the lambda-function on the webserver the end of the promise never gets reached. I assume the lambda-function stops before adding the data is executed?
This is my lambda-function:
const express = require(“express”);
const serverless = require(“serverless-http”);
const admin = require(“firebase-admin”);
let serviceAccount = require("./key.json")
if (admin.apps.length === 0) {
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://blinkstorage-dd942.firebaseio.com"
});
}
let db = admin.firestore();
function upload_request(in_file) {
console.log("reached: " + in_file.email);
let docRef = db.collection('requests').doc(in_file.email);
docRef.set({
email: in_file.email,
date: new Date().toLocaleString()
})
.then(function () {
console.log("Document successfully written!");
return 0;
})
.catch(function (error) {
console.error("Error writing document: ", error);
return 0;
});
}
// Express
const app = express();
const router = express.Router();
router.get("/", (req, res) => {
res.send('This route only takes POST requests.')
});
router.post('/', function (req, res) {
var data = JSON.parse(req.body)
upload_request(data)
res.send("Send POST worked.")
});
app.use('/.netlify/functions/send_upload', router);
module.exports.handler = serverless(app);
This is the output that is logged in the lambda-function:
7:04:56 AM: 2020-04-13T05:04:56.358Z c2e5b672-56b9-44b5-84d4-126e18db590a INFO reached: raf@test.com 7:04:56 AM: Duration: 339.92 ms Memory Usage: 112 MB Init Duration: 716.31 ms
Am I correct that Firebase might not work because the end of the promise is never reached?
Thanks for your help, I have no idea what to do right now!
Cheers
Max