I'm trying to integrate Firebase Cloud Functions into an Android app. I'm new in Cloud Functions and Node.js.
So, I have the below simple node.js code to save data in Firestore database:
'use strict';
const admin = require('firebase-admin');
const functions = require('firebase-functions');
admin.initializeApp(functions.config().firebase);
const Firestore = require('@google-cloud/firestore');
exports.saveData = functions.https.onRequest((request, response) => {
var firestoreDB = new Firestore({
projectId: 'my_project_id',
keyFilename: 'my_key_file_name'
});
const line1 = request.body.line1;
const line2 = request.body.line2;
var data = {
name: line1,
number: line2
};
return firestoreDB.collection('myData').doc('firstDoc')
.add( data )
.then(ref => {
console.log('Data saved.');
response.end();
});
});
This code is working fine, but being executed twice. It writes the data into DB twice, it logs every single log twice in Firebase console log screen.
I have tried another function to send a message using FCM from Android device to Android device, it also send the message twice.
I checked Android side to make sure it's triggered once. I'm wondering if there is anything I'm missing out to make it work. Any help or suggestions appreciated.
response.status(200).send('OK')afterconsole.log('Data saved.');?? I'm not familiar with firebase functions though. Update: Just found an example here - Molda